C#数据库未从数据集更新。SQLCommandBuilder
过去4小时一直困扰着我的问题。我的数据集得到了更改,但数据库没有 作为“全局”变量存在的东西: My form load初始化数据库连接:C#数据库未从数据集更新。SQLCommandBuilder,c#,sql-server,C#,Sql Server,过去4小时一直困扰着我的问题。我的数据集得到了更改,但数据库没有 作为“全局”变量存在的东西: My form load初始化数据库连接: objConnect = new DatabaseConnection(); conString = Properties.Settings.Default.memoDbConnectionString; objConnect.connection_string = conString;
objConnect = new DatabaseConnection();
conString = Properties.Settings.Default.memoDbConnectionString;
objConnect.connection_string = conString;
我有一个按钮,可以更改我的数据集并调用我的数据库更新
private void button5_Click(object sender, EventArgs e)
{
//
objConnect.Sql = Properties.Settings.Default.userSQL;
ds = objConnect.GetConnection;
MaxRows = ds.Tables[0].Rows.Count;
for (int i = 0; i < MaxRows; i++)
{
DataRow dRow = ds.Tables[0].Rows[i];
if (nick == dRow.ItemArray[0].ToString())
if (textBox4.Text == textBox5.Text)
if (textBox3.Text == dRow.ItemArray[1].ToString())
{
ds.Tables[0].Rows[i][1] = textBox5.Text;
//this is the interesting bit
DataSet changes = ds.GetChanges();
objConnect.UpdateDatabase(changes);
//-----
MessageBox.Show(nick);
MessageBox.Show(dRow[1].ToString());
break;
}
else MessageBox.Show("Old password is wrong.");
else MessageBox.Show("Pass confirmation does not correspond.");
}
}
da_1是一个sqlDataAdapter,我在函数外部声明它,但在函数前面的函数中赋予它属性。这个答案:
Ok, I'm going to take a guess here. Is the PacjenciDB.sdf included
into Visual Studio project by any chance? Do you have the property" Copy
to output folder" set to "Always" or something similar? It seems
that every time you do a build you could be overwriting
your output folder database file. Try putting the database in a folder
that is not inside VS project.
BTW, your code is OK.
我从邮局得到了帮助。
需要注意的一点是,如果你从调试文件夹启动程序,它将没有问题,这有助于我找到问题。问题在于,我将我的db作为数据源添加到项目中,并创建连接字符串及其相互之间的相似性。从数据源中删除它,然后从解决方案中的正确文件夹中重新添加它explorer为我修复了它。我刚刚发现它出于某种原因从一个数据库读取数据并写入另一个数据库。我的连接字符串是:Visual Studio 2015\Projects\memo\memo\Resources\memoDb.mdfIn solution explorer它说我的数据库被忽略了,实际上是在路径中的数据库。
public void UpdateDatabase(System.Data.DataSet ds)
{
System.Data.DataSet changes = ds.GetChanges();
System.Data.SqlClient.SqlCommandBuilder cb = new System.Data.SqlClient.SqlCommandBuilder(da_1);
//stuff thats suppsed to help
da_1.UpdateCommand = cb.GetUpdateCommand();
//----
da_1.Update(ds.Tables[0]);
ds.AcceptChanges();
}
Ok, I'm going to take a guess here. Is the PacjenciDB.sdf included
into Visual Studio project by any chance? Do you have the property" Copy
to output folder" set to "Always" or something similar? It seems
that every time you do a build you could be overwriting
your output folder database file. Try putting the database in a folder
that is not inside VS project.
BTW, your code is OK.