2

I have a database with multiple tables each linked to the other.(Relational Database) I want to Insert Records from my Dataset to Database. First I tried using OledbCommandBuilder but that didn't work, I found out it couldn't work with relational databases. I haven't linked tables within my dataset, they are linked only in the database. What I have is a button named "Commit". When I press that button I want all the modifications in my dataset.datatable to be reflected in the respective database table. I only know how to INSERT, UPDATE, DELETE a single record at a time but that also is a lengthy task as first I have to do this.

CODE IN THE SAVE BUTTON

             DataRow dRow = bookDS.Tables["Book"].NewRow();

               dRow[0] = Convert.ToInt64(textBookID.Text);
               dRow[1] = textBookName.Text;
               dRow[2] = textISBN.Text;

and so on (there are 30 columns)

              bookDS.Tables["Book"].Rows.Add(dRow);

Then

CODE IN THE COMMIT BUTTON

con.Open();

                    string sql = "Insert INTO Book Values (@BookId, @BookName, @ISBNNo, @PublicationId, @CategoryId, @Pages,@Price,@Author1,@Author2,@TotalCopies,@IssuedCopies,@AvailableCopies,@SupplierName,@Note)";

                    OleDbCommand save = new OleDbCommand(sql);

                    save.Parameters.Add("@BookId", OleDbType.BigInt).Value = Convert.ToInt64(dRow[0]);
                    save.Parameters.Add("@BookName", OleDbType.BSTR).Value = dRow[1];
                    save.Parameters.Add("@ISBNNo", OleDbType.BSTR).Value = dRow[2];

and so on...

save.Connection = con; save.ExecuteNonQuery();

                    con.Close();

That is all i can think of but that only works for a single Row and so I its like I have to press the COMMIT button every time I press the SAVE button.

Can anyone please help me with this ???

P.S : I am using MSAccess and also don't want to use TABLEADAPTER ( I want to do everything with my code )

4

2 回答 2

0
       string sql = "Insert INTO Book Values (@BookId, @BookName, @ISBNNo, @PublicationId, @CategoryId, @Pages,@Price,@Author1,@Author2,@TotalCopies,@IssuedCopies,@AvailableCopies,@SupplierName,@Note)";

     DataRow dRow = bookDS.Tables["Book"].NewRow();

                   dRow[0] = Convert.ToInt64(textBookID.Text);
                   dRow[1] = textBookName.Text;
                   dRow[2] = textISBN.Text;
                   bookDS.Tables["Book"].Rows.Add(dRow);
//This would help you to commit all the changes at once.   
 bookDS.AcceptChanges();
于 2012-10-26T10:19:31.733 回答
0

http://msdn.microsoft.com/en-us/library/xzb1zw3x.aspx

尝试使用 OleDBDataAdapter 来完成这项工作。或者您甚至可以尝试使用 OleDBComamndBuilder(OleDb for Access 数据库而不是 Sql):

http://www.daniweb.com/software-development/csharp/threads/380925/how-to-save-a-dataset-into-a-database

于 2012-10-26T10:16:06.447 回答