string sConnectionString; // Modify the following string to correctly connect to your SQL Server. sConnectionString = "Password=;User ID=sa;" + "Initial Catalog=pubs;" + "Data Source=(local)"; SqlConnection objConn = new SqlConnection(sConnectionString); objConn.Open(); // Create an instance of a DataAdapter. SqlDataAdapter daAuthors = new SqlDataAdapter("Select * From Authors", objConn); // Create an instance of a DataSet, and retrieve // data from the Authors table. DataSet dsPubs = new DataSet("Pubs"); daAuthors.FillSchema(dsPubs,SchemaType.Source, "Authors"); daAuthors.Fill(dsPubs,"Authors"); //**************** // BEGIN ADD CODE DataRow drCurrent; // Obtain a new DataRow object from the DataTable. drCurrent = dsPubs.Tables["Authors"].NewRow(); // Set the DataRow field values as necessary. drCurrent["au_id"] = "993-21-3427"; drCurrent["au_fname"] = "George"; drCurrent["au_lname"] = "Johnson"; drCurrent["phone"] = "800 226-0752"; drCurrent["address"] = "1956 Arlington Pl."; drCurrent["city"] = "Winnipeg"; drCurrent["state"] = "MB"; drCurrent["contract"] = 1; // Pass that new object into the Add method of the DataTable. tblAuthors.Rows.Add(drCurrent); daAuthors.UpDate(dsPubs,"Authors");
string sConnectionString; // Modify the following string to correctly connect to your SQL Server. sConnectionString = "Password=;User ID=sa;" + "Initial Catalog=pubs;" + "Data Source=(local)"; SqlConnection objConn = new SqlConnection(sConnectionString); objConn.Open();//这可以去掉 // Create an instance of a DataAdapter. SqlDataAdapter daAuthors = new SqlDataAdapter("Select * From Authors", objConn); // Create an instance of a DataSet, and retrieve // data from the Authors table. DataSet dsPubs = new DataSet("Pubs"); daAuthors.FillSchema(dsPubs,SchemaType.Source, "Authors"); daAuthors.Fill(dsPubs,"Authors"); //**************** // BEGIN ADD CODE // Create a new instance of a DataTable. DataTable tblAuthors; tblAuthors = dsPubs.Tables["Authors"]; DataRow drCurrent; // Obtain a new DataRow object from the DataTable. drCurrent = tblAuthors.NewRow(); // Set the DataRow field values as necessary. drCurrent["au_id"] = "993-21-3427"; drCurrent["au_fname"] = "George"; drCurrent["au_lname"] = "Johnson"; drCurrent["phone"] = "800 226-0752"; drCurrent["address"] = "1956 Arlington Pl."; drCurrent["city"] = "Winnipeg"; drCurrent["state"] = "MB"; drCurrent["contract"] = 1; // Pass that new object into the Add method of the DataTable. tblAuthors.Rows.Add(drCurrent); daAuthors.InsertCommand = " sql insert 语句" daAuthors.UpDate(.......)
说明你没写insertcommand,当然不能新增了。
sorry:写错了 daAuthors.InsertCommand = new SqlCommand(" sql insert 语句" , _con);
把这句SqlCommandBuilder builder = new SqlCommandBuilder( sqlDataAdapter1 ); 加再前面就可以啦// Create an instance of a DataSet, and retrieve // data from the Authors table. DataSet dsPubs = new DataSet("Pubs"); daAuthors.FillSchema(dsPubs,SchemaType.Source, "Authors"); daAuthors.Fill(dsPubs,"Authors");
sConnectionString = "Password=;User ID=sa;"
+ "Initial Catalog=pubs;"
+ "Data Source=(local)"; SqlConnection objConn
= new SqlConnection(sConnectionString);
objConn.Open(); // Create an instance of a DataAdapter.
SqlDataAdapter daAuthors
= new SqlDataAdapter("Select * From Authors", objConn); // Create an instance of a DataSet, and retrieve
// data from the Authors table.
DataSet dsPubs = new DataSet("Pubs");
daAuthors.FillSchema(dsPubs,SchemaType.Source, "Authors");
daAuthors.Fill(dsPubs,"Authors");
//****************
// BEGIN ADD CODE DataRow drCurrent;
// Obtain a new DataRow object from the DataTable.
drCurrent = dsPubs.Tables["Authors"].NewRow(); // Set the DataRow field values as necessary.
drCurrent["au_id"] = "993-21-3427";
drCurrent["au_fname"] = "George";
drCurrent["au_lname"] = "Johnson";
drCurrent["phone"] = "800 226-0752";
drCurrent["address"] = "1956 Arlington Pl.";
drCurrent["city"] = "Winnipeg";
drCurrent["state"] = "MB";
drCurrent["contract"] = 1; // Pass that new object into the Add method of the DataTable.
tblAuthors.Rows.Add(drCurrent); daAuthors.UpDate(dsPubs,"Authors");
添加daAuthors.UpDate(dsPubs,"Authors"); 一行后
报错:当传递具有新行的DataRow集合时,更新要求有效的InsertCommand
请各位大虾帮忙再看一下!
sConnectionString = "Password=;User ID=sa;"
+ "Initial Catalog=pubs;"
+ "Data Source=(local)"; SqlConnection objConn
= new SqlConnection(sConnectionString);
objConn.Open();//这可以去掉 // Create an instance of a DataAdapter.
SqlDataAdapter daAuthors
= new SqlDataAdapter("Select * From Authors", objConn); // Create an instance of a DataSet, and retrieve
// data from the Authors table.
DataSet dsPubs = new DataSet("Pubs");
daAuthors.FillSchema(dsPubs,SchemaType.Source, "Authors");
daAuthors.Fill(dsPubs,"Authors");
//****************
// BEGIN ADD CODE
// Create a new instance of a DataTable.
DataTable tblAuthors;
tblAuthors = dsPubs.Tables["Authors"]; DataRow drCurrent;
// Obtain a new DataRow object from the DataTable.
drCurrent = tblAuthors.NewRow(); // Set the DataRow field values as necessary.
drCurrent["au_id"] = "993-21-3427";
drCurrent["au_fname"] = "George";
drCurrent["au_lname"] = "Johnson";
drCurrent["phone"] = "800 226-0752";
drCurrent["address"] = "1956 Arlington Pl.";
drCurrent["city"] = "Winnipeg";
drCurrent["state"] = "MB";
drCurrent["contract"] = 1; // Pass that new object into the Add method of the DataTable.
tblAuthors.Rows.Add(drCurrent);
daAuthors.InsertCommand = " sql insert 语句"
daAuthors.UpDate(.......)
daAuthors.InsertCommand = new SqlCommand(" sql insert 语句" , _con);
的command来更新数据库
加再前面就可以啦// Create an instance of a DataSet, and retrieve
// data from the Authors table.
DataSet dsPubs = new DataSet("Pubs");
daAuthors.FillSchema(dsPubs,SchemaType.Source, "Authors");
daAuthors.Fill(dsPubs,"Authors");