I try to update a sqldatabase server. I did this earlier with the buildin "Local Database" in visual studio. B I did this (in short)
System.Data.SqlServerCe.SqlCeCommandBuilder cb;
cb = new System.Data.SqlServerCe.SqlCeCommandBuilder(da);
cb.DataAdapter.Update(ds1, "Alarmen");
But the builtin database did not meet the requirements. I had to use Microsoft SQL server 2005. I tried to do this also, but that did not work. First i fill a database set from the database with the following commands:\
fillDataSet()
{
SqlConnection _con = new SqlConnection("server=(local)\\SQLExpress;database=Alarmen;integrated Security=SSPI;");
string sql = "SELECT * FROM tbl_alarmen";
try
{
_con.Open();
}
catch (Exception ex)
{
log.Info("Database kon niet geopend worden " + ex);
}
DataSet ds1 = new DataSet();
try
{
sql = "SELECT * FROM dbo.tbl_alarmen";
_cmd = new SqlCommand(sql, _con);
_dap = new SqlDataAdapter(_cmd);
_dap.Fill(ds1, "Alarmen");
foreach (DataRow drow in ds1.Tables["Alarmen"].Rows)
{
log.Info("Test");
}
}
catch (Exception err)
{
log.Info("Database lezen mislukt " + err);
}
try
{
_con.Close();
}
catch (Exception ex)
{
log.Info("Database sluiten mislukt " + ex);
}
_dap.Fill(ds1, "Alarmen");
foreach (DataRow drow in ds1.Tables["Alarmen"].Rows)
{
log.Info("Test");
}
}
catch (Exception err)
{
log.Info("Database lezen mislukt " + err);
}
try
{
_con.Close();
}
catch (Exception ex)
{
log.Info("Database sluiten mislukt " + ex);
}
}
Then i add rows and modify items in the dataset and try op update the changes in the database.
UpdateDatabase()
{
SqlConnection _con = new SqlConnection("server=(local)\\SQLExpress;database=Alarmen;integrated Security=SSPI;");
string sql = "SELECT * FROM tbl_alarmen";
try
{
_con.Open();
}
catch (Exception ex)
{
log.Info("Database kon niet geopend worden " + ex);
}
try
{
var con = new SqlConnection(connectionString);
var adapter = new SqlDataAdapter("SELECT * FROM tbl_alarmen", con);
new SqlCommandBuilder(adapter);
//
// Fill the DataAdapter with the values in the DataTable.
//
adapter.Fill(ds1);
//
// Insert the data table into the SQL database.
//
adapter.Update(ds1);
}
catch (Exception e)
{
log.Info("Fill database failed " + e);
}
try
{
_con.Close();
}
catch (Exception ex)
{
log.Info("Database sluiten mislukt " + ex);
}
}
I do not get any errors, but the database keeps being empty.
I now i should not use the "SELECT *" for security issues. I need to change that in the future.
I see i did not aks a real question. What did i do wrong in my code and how could i fix it.