新增一笔记录,为什么在DataGrid中可以看到,而在数据库中找不到呢?(在线等待) ( 积分: 30 )

  • 主题发起人 主题发起人 SharpChen
  • 开始时间 开始时间
S

SharpChen

Unregistered / Unconfirmed
GUEST, unregistred user!
如题
以下是执行的相关代码,请大侠们帮忙看一下有什么问题吗?小弟谢谢了
private void Insert_Click(object sender, System.EventArgs e)
{
DataTable myDataTable = myDataSet.Tables["Customers"];
DataRow myDataRow = myDataTable .NewRow();
myDataRow["CustomerID"] = "T1COM";
myDataRow["CompanyName"] = "T1 Company";
myDataRow["ContactName"] = "Jason Price";
myDataRow["Address"] = "1 Main Street";
myDataTable.Rows.Add(myDataRow);
myDataTable.AcceptChanges();
SqlCommandBuilder objCommandBuilder = new SqlCommandBuilder(mySqlDataAdapter);
mySqlDataAdapter.Update(myDataSet,"Customers");
mySqlDataAdapter.Fill(myDataSet,"Customers");
}
 
如题
以下是执行的相关代码,请大侠们帮忙看一下有什么问题吗?小弟谢谢了
private void Insert_Click(object sender, System.EventArgs e)
{
DataTable myDataTable = myDataSet.Tables["Customers"];
DataRow myDataRow = myDataTable .NewRow();
myDataRow["CustomerID"] = "T1COM";
myDataRow["CompanyName"] = "T1 Company";
myDataRow["ContactName"] = "Jason Price";
myDataRow["Address"] = "1 Main Street";
myDataTable.Rows.Add(myDataRow);
myDataTable.AcceptChanges();
SqlCommandBuilder objCommandBuilder = new SqlCommandBuilder(mySqlDataAdapter);
mySqlDataAdapter.Update(myDataSet,"Customers");
mySqlDataAdapter.Fill(myDataSet,"Customers");
}
 
少写了一些代码
SqlCommandBuilder objCommandBuilder = new SqlCommandBuilder(mySqlDataAdapter);
mySqlDataAdapter.InsertCommand=objCommandBuilder.GetInsertCommand();
 
To ardorman:
加上了你的那些代码还是不行,没有返回给数据库相应的插入Sql,不过,我想问题可能还是出在mySqlDataAdapter或是SqlCommandBuilder 中!
 
private void Insert_Click(object sender, System.EventArgs e)
{
DataTable myDataTable = myDataSet.Tables["Customers"];
DataRow myDataRow = myDataTable .NewRow();
myDataRow["CustomerID"] = "T1COM";
myDataRow["CompanyName"] = "T1 Company";
myDataRow["ContactName"] = "Jason Price";
myDataRow["Address"] = "1 Main Street";
myDataTable.Rows.Add(myDataRow);
//myDataTable.AcceptChanges();
SqlCommandBuilder objCommandBuilder = new SqlCommandBuilder(mySqlDataAdapter);
mySqlDataAdapter.Update(myDataSet,"Customers");
mySqlDataAdapter.Fill(myDataSet,"Customers");
}
去掉myDataTable.AcceptChanges();这一行就可以了,因为在update是会检测dataset中更改的行,一段acceptchanges了,就没法识别那些记录是add,modify等了
 
接受答案了.
 
后退
顶部