简体   繁体   English

无法在C#中使用SqlDataAdapter和MSSqlServer插入数据库

[英]Can not Insert into Database with SqlDataAdapter and MSSqlServer in c#

I want to insert new record into the DataTable with DataAdapter. 我想使用DataAdapter将新记录插入到DataTable中。 There are 2 columns in Person table. 人员表中有2列。 FirstName and LastName. 名字和姓氏。
I have a code like below. 我有下面的代码。

SqlConnection connection;
DataSet dsPerson;
SqlDataAdapter adapter;
string connectionString = Properties.Resources.ConnectionString;
connection = new SqlConnection(connectionString);

dsPerson = new DataSet("PersonDataSet");
adapter = new SqlDataAdapter();
SqlCommand selectCommand = new SqlCommand("SELECT * FROM Person", connection);
adapter.SelectCommand = selectCommand;
adapter.Fill(dsPerson, "Person");

string insertQuery = "INSERT INTO Person(FirstName, LastName) VALUES (@FirstName, @LastName)";
SqlCommand insertCommand = new SqlCommand(insertQuery, connection);

insertCommand.Parameters.Add(new SqlParameter("@FirstName", SqlDbType.NVarChar));
insertCommand.Parameters["@FirstName"].SourceVersion = DataRowVersion.Current;
insertCommand.Parameters["@FirstName"].SourceColumn = "FirstName";

insertCommand.Parameters.Add(new SqlParameter("@LastName", SqlDbType.NVarChar));
insertCommand.Parameters["@LastName"].SourceVersion = DataRowVersion.Current;
insertCommand.Parameters["@LastName"].SourceColumn = "LastName";

adapter.InsertCommand = insertCommand;

But when I try to insert new Record into DataSet it appears in the DataSet. 但是,当我尝试将新的Record插入DataSet时,它会出现在DataSet中。 But it does not do insert new record into DataBase. 但是它不会将新记录插入数据库。
The Code for Insert is below. 插入代码如下。

connection.Open();
DataRow newRow = dsPerson.Tables["Person"].NewRow();
newRow["FirstName"] = "Joseph";
newRow["LastName"] = "Sword";
dsPerson.Tables["Person"].Rows.Add(newRow);
dsPerson.Tables["Person"].AcceptChanges();
dsPerson.AcceptChanges();
adapter.Update(dsPerson, "Person");
connection.Close();

I even try to trace queries sent to sql server With Express Profiler. 我什至尝试使用Express Profiler跟踪发送到sql服务器的查询。 And i saw that it does not send insert command to the database. 而且我看到它不会向数据库发送插入命令。
So what is the problem? 那是什么问题呢? How to solve it? 怎么解决呢?
Thank you. 谢谢。

You should not call AcceptChanges methods of dataset and DataTable because DataAdapter.Update method affects only changed/added/deleted rows from datatable. 您不应调用数据集和DataTable的AcceptChanges方法,因为DataAdapter.Update方法仅影响数据表中已更改/添加/删除的行。

But after calling AcceptChanges all your DataRows will have Unchanged state. 但是,在调用AcceptChanges之后,所有的DataRows都将处于Unchanged状态。

See MSDN for reference about DataAdapter: 请参阅MSDN以获取有关DataAdapter的参考:

When an application calls the Update method, the DataAdapter examines the RowState property, and executes the required INSERT, UPDATE, or DELETE statements iteratively for each row, based on the order of the indexes configured in the DataSet. 当应用程序调用Update方法时,DataAdapter将检查RowState属性,并根据在DataSet中配置的索引的顺序,为每行迭代执行所需的INSERT,UPDATE或DELETE语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM