我正在做一个简单的小程序,想把用户输入的数据添加到后台数据库中。我保证数据库连接得没问题。但只要更新数据库就有问题了。我只要调用下面这一个子程序,运行到语句'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();
}很着急呀,明天就要交程序了,大家帮帮忙亚。

解决方案 »

  1.   

    去看一看
    ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.2052/cpref/html/frlrfsystemdatacommondbdataadapterclassupdatetopic.htm你好像少了fill
      

  2.   

    楼主要不就用insert into语句了
    根据你的字段,应该是这样写:
    我用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()
      

  3.   

    谢谢大家,我现在还有一个小问题就是我的数据怎么写不回原来的access数据库呀?我试了两种方法一种是用DataSet, 一种是用insertCommand.都不行。现在的情况是,我可以往对话框中输入数据,再点确认键,程序不报错,但是检查access数据库相应表格的数据没有变化。我怀疑是不是要对access数据库进行什么修改全县的设置亚?还是我的代码有问题?我的代码贴在下面了:非常着急,非常着急,请大家帮帮我把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();     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();
    }