private void Page_Load(object sender, System.EventArgs e)
{
string connStr="server=NicholasLee;uid=sa;pwd=sa;database=myWebDB";
SqlConnection myConnection = new SqlConnection(connStr);
string queryStr="select * from information";
SqlDataAdapter myDataAdapter = new SqlDataAdapter(queryStr,myConnection);
DataSet myDataSet = new DataSet();
myDataAdapter.Fill(myDataSet,"information");
//清除上一次插入的记录
Cleanup();
//在数据集插入一条新记录
SqlCommandBuilder mySqlCommandBuilder=new SqlCommandBuilder(myDataAdapter);
//使用CommandBuilder自动生成SQL语句
DataRow myDataRow = myDataSet.Tables["information"].NewRow();
myDataRow["title"]="新title";
myDataRow["author"]="新author";
myDataRow["content"]="新content";
myDataRow["datetime"]=System.DateTime.Now;
myDataSet.Tables["information"].Rows.Add(myDataRow);
myDataAdapter.Update(myDataSet,"information");
Response.Write("已经完成更新");
}
//添加自定义函数Cleanup()
private void Cleanup()
{
string connStr="server=NicholasLee;uid=sa;pwd=sa;database=myWebDB";
SqlConnection myConnection=new SqlConnection(connStr);
myConnection.Open();
string delStr="DELETE FROM information WHERE title='新title'";
SqlCommand myCommand = new SqlCommand(delStr,myConnection);
myCommand.ExecuteNonQuery();
myConnection.Close();
}
错误提示是:
源错误:
行 42: myDataSet.Tables["information"].Rows.Add(myDataRow);
行 43:
行 44: myDataAdapter.Update(myDataSet,"information");
行 45: Response.Write("已经完成更新");
行 46: }
请大虾们告诉我怎么解决该问题.