0

I am learning how to work with SQL in C#, and I got in troubles with using SqlDataAdapter. I have tried to use direct queries via SqlCommand class and everything works fine, but when I rewrote my code to use SqlDataAdapter I have no changes in my table. There is my code:

SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ADO"]
                                                                 .ConnectionString);

connection.Open();

SqlDataAdapter daUser = new SqlDataAdapter("SELECT * FROM Books", connection);

SqlCommand insert = new SqlCommand();
insert.Connection = connection;
insert.CommandText = "INSERT INTO Books (name, author) VALUES (@name, @author);";

SqlParameterCollection pc = insert.Parameters;
pc.Add("@name", SqlDbType.VarChar, 20, "test123");
pc.Add("@author", SqlDbType.VarChar, 20, "test322");
daUser.InsertCommand = insert;

DataSet ds = new DataSet();
daUser.Fill(ds, "Books");
daUser.Update(ds, "Books");

Table Books was created with this SQL query in SQL Server Management Studio:

CREATE TABLE Books 
(
    id int PRIMARY KEY IDENTITY(1,1),
    name varchar(MAX) NOT NULL,
    author varchar(MAX) NOT NULL
)

INSERT INTO Books(name, author) 
VALUES('1984', 'George Orwell'), ('Fathers and sons', 'Dostoevski')

Looks like I am missing something to do, that why my code have no effect on table.

2
  • Possible duplicate of Using SqlDataAdapter to insert a row Commented Jan 11, 2016 at 14:23
  • If you want to use an SqlDataAdapter in this context (not the correct way to do it) then you need to add your data to the table returned by the Fill method. SqlDataAdapter.Update method looks at the data changed in that table to decide what to update/insert or delete. You don't have any change there so no INSERT is called (of course you don't need the parameters) Commented Jan 11, 2016 at 14:23

2 Answers 2

2

SqlDataAdapter.Update will call its InsertCommand only for the rows of datatable having RowState = DataRowState.Added.

This rowstate is automatically assigned to the datarows being added to rows collection using DataTable.Add method (until next call to AcceptChanges method). Also you can use DataRow.SetAdded method to force this state assignment.

Since you're not modifying/adding anything in you datatable after you've populated it with select command, it has nothing to insert.

Change your code to something like

daUser.Fill(ds, "Books");

var newBook = daUser.Tables[0].NewRow();
newBook["name"] = "New Book";
newBook["author"] = "Author Name";
daUser.Tables[0].Rows.Add(newBook);

daUser.Update(ds, "Books");

and in this case it should be new row added to the database table.

See MSDN for reference.

Sign up to request clarification or add additional context in comments.

Comments

-1

Just to clarify the previous answer, which is correct, you want to call ExecuteNonQuery() on the command not the dataAdapter.

SqlCommand insert = new SqlCommand(); 
insert.Connection = connection;
insert.CommandText = "INSERT INTO Books (name, author) VALUES (@name,
@author);";
SqlParameterCollection pc = insert.Parameters;
pc.Add("@name", SqlDbType.VarChar, 20, "test123"); 
pc.Add("@author",
SqlDbType.VarChar, 20, "test322"); 
// you do not need this line if you execute the insert on the command object.
// daUser.InsertCommand = insert;

//Add this line instead:
insert.ExecuteNonQuery();

Joey

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.