private void Button1_Click(object sender, System.EventArgs e)
{
   OleDbConnection mycon=new OleDbConnection(conStr);
   try
     {
mycon.Open();
OleDbCommand cmd= new OleDbCommand();
cmd.Connection=mycon;      
         cmd.CommandText="update userinfo set username=@uasename,userpwd=@pwd,realname=@realname, gender=@gender,age=@age,userTel=@Tel,userAddr=@userAddr,userEmail=@Email where userid=@userid";
        
         cmd.Parameters.Add("@username",OleDbType.BSTR).Value=TextBox1.Text;
cmd.Parameters.Add("@pwd",OleDbType.BSTR).Value=TextBox2.Text;
cmd.Parameters.Add("@realname",OleDbType.BSTR).Value=TextBox4.Text;
cmd.Parameters.Add("@gender",OleDbType.BSTR).Value=TextBox5.Text;
cmd.Parameters.Add("@age",OleDbType.Integer).Value=Convert.ToInt16(TextBox6.Text);
cmd.Parameters.Add("@Tel",OleDbType.BSTR).Value=TextBox7.Text;
cmd.Parameters.Add("@userAddr",OleDbType.BSTR).Value=TextBox8.Text;
cmd.Parameters.Add("@Email",OleDbType.BSTR).Value=TextBox9.Text;
cmd.Parameters.Add("@userid",OleDbType.Integer).Value=Convert.ToInt16(userid);
         cmd.Prepare();
int m=cmd.ExecuteNonQuery(); if(m==1)
   strReginfo="更新成功!";
else
   strReginfo="更新失败!";
}
    catch(Exception sqlerr)
        {
   throw new Exception(sqlerr.Message);
}
finally
{
   mycon.Close();
}
}

解决方案 »

  1.   

    出错地方在:
      int m=cmd.ExecuteNonQuery();
      

  2.   

    public DataSet CreateCmdsAndUpdate(DataSet myDataSet,string myConnection,string mySelectQuery,string myTableName) 
    {
        OleDbConnection myConn = new OleDbConnection(myConnection);
        OleDbDataAdapter myDataAdapter = new OleDbDataAdapter();
        myDataAdapter.SelectCommand = new OleDbCommand(mySelectQuery, myConn);
        OleDbCommandBuilder custCB = new OleDbCommandBuilder(myDataAdapter);    myConn.Open();    DataSet custDS = new DataSet();
        myDataAdapter.Fill(custDS);    //code to modify data in dataset here    myDataAdapter.Update(custDS, myTableName);    myConn.Close();    return custDS;
     }
      

  3.   

    public void OleDbCommandPrepareEx() {
       int  id = 20;
       string  desc = "myFirstRegion" ;
       OleDbConnection rConn = new OleDbConnection("Provider=SQLOLEDB;Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Northwind;");
       rConn.Open();
       OleDbCommand command   = new OleDbCommand(null, rConn);   // Using the NorthWind database ...
       command.CommandText = "insert into Region (RegionID, RegionDescription) values (@id, @desc)" ;
       command.Parameters.Add ( "@id", id) ;
       command.Parameters.Add ( "@desc", desc) ;
       command.Prepare() ;  // Calling Prepare after having setup commandtext and params.
       command.ExecuteNonQuery();   // Change param values and call execute. This time the prepared command will be executed.
       command.Parameters[0].Value = 21;
       command.Parameters[1].Value = "mySecondRegion";
       command.ExecuteNonQuery();
    }
      

  4.   

    用我的程序读取SQL Server 数据库时没有任何问题
    只是在更新Access时会出问题,但读取Access中的数据时也不会出现任何问题
    是不是哪一个配置有问题?