我正在做一个简单的小程序,想把用户输入的数据添加到后台数据库中。我保证数据库连接得没问题。但只要更新数据库就有问题了。我只要调用下面这一个子程序,运行到语句'dataAdapter.Update(myDataSet, "city");'这句话时就有问题了。我觉得之前应该都没错,我可以跟踪到newRow的值。protected void button1_Click(object sender, System.EventArgs e)
{
DataRow newRow = dataTable.NewRow();
newRow["name"] = cityBox.Text;
newRow["country"]=countryBox.Text;
newRow["ID"] = IDBox.Text; dataTable.Rows.Add(newRow);
dataAdapter.Update(myDataSet, "city"); Application.DoEvents();
myDataSet.AcceptChanges(); PopulateLB();
ClearFields();
}很着急呀,明天就要交程序了,大家帮帮忙亚。
{
DataRow newRow = dataTable.NewRow();
newRow["name"] = cityBox.Text;
newRow["country"]=countryBox.Text;
newRow["ID"] = IDBox.Text; dataTable.Rows.Add(newRow);
dataAdapter.Update(myDataSet, "city"); Application.DoEvents();
myDataSet.AcceptChanges(); PopulateLB();
ClearFields();
}很着急呀,明天就要交程序了,大家帮帮忙亚。
ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.2052/cpref/html/frlrfsystemdatacommondbdataadapterclassupdatetopic.htm你好像少了fill
根据你的字段,应该是这样写:
我用VB写的,我不会C,你改一下就可以
Dim sql as string
Dim Da as SqlDataAdapter
Dim Ds as New DataSet()sql="Insert Into city(name,contry,ID) values('" & cityBox.Text & "','" & countryBox.Text & "',' " & IDBox.Text & "')"
Da=New SqlDataAdapter(sql,conn)
Da.Fill(Ds,"add")
Da.Dispose()
conn.close()
{
string cityText = cityTextBox.Text;
string countryText = countryTextBox.Text;
string idText = IDTextBox.Text; myDataSet = (DataSet)Session["MyData"];
string strConnection ="Provider = Microsoft.Jet.OleDb.4.0;";
strConnection +=@"Data Source = C:\Visual Studio Projects\database\citydab.mdb"; myConnection = new System.Data.OleDb.OleDbConnection(strConnection);
myConnection.Open(); myCommand = new System.Data.OleDb.OleDbCommand();
myCommand.Connection = myConnection;
string command = "insert into city(name, country, ID) values (cityText, countryText, idText)";
myCommand = new System.Data.OleDb.OleDbCommand(command, myConnection); DataAdapter = new System.Data.OleDb.OleDbDataAdapter();
DataAdapter.InsertCommand = myCommand;
DataAdapter.Dispose();
myConnection.Close();
}还有一种方法是用DataSet:
protected void addButton_Click(object sender, System.EventArgs e)
{
string cityText = cityTextBox.Text;
string countryText = countryTextBox.Text;
string idText = IDTextBox.Text; myDataSet = (DataSet)Session["MyData"];
string strConnection ="Provider = Microsoft.Jet.OleDb.4.0;";
strConnection +=@"Data Source = C:\Visual Studio Projects\database\citydab.mdb"; myConnection = new System.Data.OleDb.OleDbConnection(strConnection);
myConnection.Open(); this.t1 = myDataSet.Tables[0];
this.r = this.t1.NewRow(); this.r["name"] = cityText;
this.r["country"] = countryText;
this.r["ID"] = idText;
this.t1.Rows.Add(r);
myDataSet.AcceptChanges();
DataAdapter = new System.Data.OleDb.OleDbDataAdapter();
DataAdapter.Update(myDataSet, "city");
myConnection.Close();
}