C# SQL Server update
本文关键字:update Server SQL | 更新日期: 2023-09-27 18:18:12
我尝试更新sqldatabase服务器。我之前在visual studio中使用了内置的"本地数据库"。B我做了这个(简而言之)
System.Data.SqlServerCe.SqlCeCommandBuilder cb;
cb = new System.Data.SqlServerCe.SqlCeCommandBuilder(da);
cb.DataAdapter.Update(ds1, "Alarmen");
但是内置的数据库不符合要求。我必须使用微软SQL server 2005。我也试过这样做,但没有成功。首先,我用以下命令填充数据库中的数据库集:'
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);
}
}
然后我在数据集中添加行和修改项,并尝试op更新数据库中的更改。
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);
}
}
我没有得到任何错误,但数据库一直是空的。
我现在不应该使用"SELECT *"的安全问题。我需要在将来改变它。
我知道我没有问一个真正的问题。我在代码中做错了什么,我该如何修复它Try
adapter.Fill(ds1, "Alarmen")
和
adapter.Update(ds1, "Alarmen");
查看此MS链接获取更多信息