插入值参数化查询 [英] Parameterized query for inserting values

查看:107
本文介绍了插入值参数化查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图插入使用参数化查询值到Access数据库:

 私人无效的button1_Click(对象发件人,EventArgs的发送)
        {
            如果(validationcontrol())
            {
                MessageBox.Show(cmbjob code.SelectedValue.ToString());
                OleDbConnection的oleDbConnection1 =新System.Data.OleDb.OleDbConnection(CONNSTRING);
                oleDbConnection1.Open();
                OleDbCommand的oleDbCommand1 =新System.Data.OleDb.OleDbCommand(INSERT INTO quotationmastertable(报价code,工作code,jobpk,sillabordercharges,battabordercharges,driverpayment,租金,额外的,总共有折扣,此言一出,量)值oleDbConnection1)(,,,,,,,,,,,????????????);
                oleDbCommand1.Parameters.Add(txtquotationno.Text);
                oleDbCommand1.Parameters.Add(cmbjob code.Text);
                oleDbCommand1.Parameters.Add(cmbjob code.SelectedValue);
                oleDbCommand1.Parameters.Add(int.Parse(txtsilabordercharges.Text));
                oleDbCommand1.Parameters.Add(int.Parse(txtbattacharges.Text));
                oleDbCommand1.Parameters.Add(int.Parse(txtdriverpayment.Text));
                oleDbCommand1.Parameters.Add(int.Parse(txtrent.Text));
                oleDbCommand1.Parameters.Add(int.Parse(txtextra.Text));
                oleDbCommand1.Parameters.Add(int.Parse(txttotal.Text));
                oleDbCommand1.Parameters.Add(int.Parse(txtdiscount.Text));
                oleDbCommand1.Parameters.Add(txtremark.Text);
                oleDbCommand1.Parameters.Add(int.Parse(txtamount.Text));
                oleDbCommand1.CommandType = CommandType.Text;
                oleDbCommand1.ExecuteNonQuery();
                oleDbConnection1.Close();
                MessageBox.Show(txtquotationno.Text);            }
        }

但我在第一线本身变得异常

  oleDbCommand1.Parameters.Add(txtquotationno.Text);

唯一的例外是


  

该OleDbParameterCollection只接受非空OleDbParameter类型的对象,而不是String对象。


我是新来编程;任何人都可以在指出我的错误帮助?


解决方案

添加对象的单个参数期待一个OleDBParameter对象。你只是传递字符串和数据。

有一个简单的解决将是使用 AddWithValue 方法:

  oleDbCommand1.Parameters.AddWithValue(,txtquotationno.Text?);
oleDbCommand1.Parameters.AddWithValue(,cmbjob code.Text?);

OLEDB并没有真正使用的参数名称,它的指数化,这就是为什么你可以通过问号您的参数作为名称中的每一个。您必须确保您的参数是相同的顺序您的查询语句。

I was trying to insert values into an Access database using a parameterized query:

private void button1_Click(object sender, EventArgs e)
        {
            if (validationcontrol())
            {
                MessageBox.Show(cmbjobcode.SelectedValue.ToString());
                OleDbConnection oleDbConnection1 = new System.Data.OleDb.OleDbConnection(connString);
                oleDbConnection1.Open();
                OleDbCommand oleDbCommand1 = new System.Data.OleDb.OleDbCommand("INSERT INTO quotationmastertable (quotationcode ,jobcode , jobpk , sillabordercharges , battabordercharges , driverpayment , rent , extra , total , discount , remark ,amount ) Values (?,?,?,?,?,?,?,?,?,?,?,?) ", oleDbConnection1);
                oleDbCommand1.Parameters.Add(txtquotationno.Text);
                oleDbCommand1.Parameters.Add(cmbjobcode.Text);
                oleDbCommand1.Parameters.Add(cmbjobcode.SelectedValue);
                oleDbCommand1.Parameters.Add(int.Parse(txtsilabordercharges.Text));
                oleDbCommand1.Parameters.Add(int.Parse(txtbattacharges.Text));
                oleDbCommand1.Parameters.Add(int.Parse(txtdriverpayment.Text));
                oleDbCommand1.Parameters.Add(int.Parse(txtrent.Text));
                oleDbCommand1.Parameters.Add(int.Parse(txtextra.Text));
                oleDbCommand1.Parameters.Add(int.Parse(txttotal.Text));
                oleDbCommand1.Parameters.Add(int.Parse(txtdiscount.Text));
                oleDbCommand1.Parameters.Add(txtremark.Text);
                oleDbCommand1.Parameters.Add(int.Parse(txtamount.Text));
                oleDbCommand1.CommandType = CommandType.Text;
                oleDbCommand1.ExecuteNonQuery();
                oleDbConnection1.Close();
                MessageBox.Show(txtquotationno.Text);

            }
        }

but I am getting an exception at the first line itself:

oleDbCommand1.Parameters.Add(txtquotationno.Text);

The exception is

The OleDbParameterCollection only accepts non-null OleDbParameter type objects, not String objects.

I am new to programming; can anyone help in pointing out my mistakes?

解决方案

A single parameter for the Add object is expecting an OleDBParameter object. You are just passing strings and data.

A simple fix would be to use the AddWithValue method:

oleDbCommand1.Parameters.AddWithValue("?", txtquotationno.Text);
oleDbCommand1.Parameters.AddWithValue("?", cmbjobcode.Text);

OleDB does not really use parameter names, it's index based, which is why you can pass the question mark for each one of your parameters as the name. You do have to make sure your parameters are in the same order as your query statement.

这篇关于插入值参数化查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆