【问题标题】:c# access database update valuec#访问数据库更新值
【发布时间】:2020-04-20 19:15:57
【问题描述】:

我尝试更新这样的数据值

 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();



        }

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

【问题讨论】:

  • 连接工作正常。
  • 我记得访问不支持命名参数。尝试将您的查询更改为“Update [TBL_BOT] set [botname]=?, [botpass]=?, [botcha]=? where [botid]=?”然后按正确的顺序添加参数Parameters.AddWithValue()
  • 呃,但是当它的时候我怎么能订购一个值呢?是不是像 komut.parameters.addwithvalue(botname, "value" );

标签: c# oledbcommand


【解决方案1】:

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

      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```


【讨论】:

  • 我尝试阅读时没有任何问题。我可以读取访问数据库也可以插入和删除。但是当我尝试更新值时
【解决方案2】:

尝试将参数顺序从@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 块中。

【讨论】:

    【解决方案3】:

    是的,谢谢大家,我浪费了你的时间:(对不起。

    我写错了标题文本 :D botname must be botacc :D

    【讨论】:

      猜你喜欢
      • 2021-03-23
      • 1970-01-01
      • 2013-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多