c#访问数据库更新值

问题描述 投票:0回答:3

我试图更新这样的数据值

 OleDbConnection baglanti = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data 
 Source=.\botbase.accdb");
  private void metroButton4_Click(object sender, EventArgs e)
        {
            baglanti.Open();
            OleDbCommand komut = new OleDbCommand("Update TBL_BOT set botname=@p1, botpass=@p2,botcha=@p3 where botid = @p4 ", baglanti);
            komut.Parameters.AddWithValue("@p4", metroTextBox6.Text);
            komut.Parameters.AddWithValue("@p1", metroTextBox3.Text);
            komut.Parameters.AddWithValue("@p2", metroTextBox4.Text);
            komut.Parameters.AddWithValue("@p3", metroTextBox5.Text);
            komut.ExecuteNonQuery();
            baglanti.Close();



        }

当我运行它时。出现这样的错误

enter image description here

c# oledbcommand
3个回答
0
投票

您可能需要传递数据类型。像这样的东西:

      public  AddressVerify AddVerify(ref AddressVerify thisAddress)
        {
            using (SqlConnection connection = new SqlConnection(connStr))
            {
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = "verifyAddress";
                cmd.Parameters.Add("@City", SqlDbType.VarChar).Value = thisAddress.city;
                cmd.Parameters.Add("@State", SqlDbType.VarChar).Value = thisAddress.state;
                cmd.Parameters.Add("@zipCode", SqlDbType.VarChar).Value = thisAddress.zipCode;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Connection = connection;
                connection.Open();
                SqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    thisAddress.Status = (string)(reader["Status"]);
                }//while
                reader.Close();
            }//using
            return thisAddress;
        }//AddVerify```



0
投票

尝试将参数顺序从@ p1更改为@ p4:

static void Main(string[] args)
{
    using (OleDbConnection baglanti = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\tomas.paul\Documents\botbase.accdb"))
    using (OleDbCommand komut = new OleDbCommand("Update TBL_BOT set botname=@p1, botpass=@p2,botcha=@p3 where botid = @p4", baglanti))
    {
        baglanti.Open();
        komut.Parameters.AddWithValue("@p1", "parameter 1");
        komut.Parameters.AddWithValue("@p2", "parameter 2");
        komut.Parameters.AddWithValue("@p3", "parameter 3");
        komut.Parameters.AddWithValue("@p4", "1");
        komut.ExecuteNonQuery();
        baglanti.Close();
    }
}

这对我有用。我已将执行代码添加到推荐的using块中。


0
投票

是对我浪费时间的每个人的感谢:(对不起。

我写错了标题文本:D botname必须是botacc:D

© www.soinside.com 2019 - 2024. All rights reserved.