用于插入值的参数化查询 [英] parametrised query for inserting values

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

问题描述

我试图通过参数化方法将值插入到acess数据库中


I was trying to insert values into acess database by parameterised method


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

               }
           }



但我在第一行本身就遇到了异常


oleDbCommand1.Parameters.Add(txtquotationno.Text);
例外是

OleDbParameterCollection仅接受非null的OleDbParameter类型的对象,而不接受String对象.

我是编程新手.



but I am getting exception at the first line itself


oleDbCommand1.Parameters.Add(txtquotationno.Text);
exception is

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

I am new to programming. Can anyone help by pointing out my mistakes?

推荐答案

当然可以.异常消息非常有用,您只需遵循它即可.

您需要了解参数不是参数值.它实质上是参数的元数据描述,当您在参数化查询中替换值时,它可以帮助ADO.NET处理实际参数值.

这样,方法System.Data.OleDb.OleDbCommand.Parameters.Add需要类型为System.Data.OleDb.OleDbParameter的参数.请参阅:
http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbcommand.parameters.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system. data.oledb.oledbparameter.aspx [ ^ ].

您可能需要了解所有原理.请阅读:
http://msdn.microsoft.com/en-us/library/yy6y35y8.aspx [ ^ ].

—SA
Of course. The exception message is quite informative, you should just follow it.

You need to understand that a parameter is not a parameter value. It is essentially a meta-data description of the parameter which helps the ADO.NET to process actual parameter value when you substitute the value in the parametrized query.

In this way, the method System.Data.OleDb.OleDbCommand.Parameters.Add expect the parameter of the type System.Data.OleDb.OleDbParameter. Please see:
http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbcommand.parameters.aspx[^],
http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbparameter.aspx[^].

You probably need to understand how it all works in principle. Please read:
http://msdn.microsoft.com/en-us/library/yy6y35y8.aspx[^].

—SA


您好..
试试这个.

Hi there..
Try this.

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(@quotationcode ,@jobcode , @jobpk , @sillabordercharges , @battabordercharges , @driverpayment , @rent , @extra , @total , @discount , @remark ,@amount ) ", oleDbConnection1);
                    oleDbCommand1.Parameters.Add("@quotationcode", txtquotationno.Text);
                    oleDbCommand1.Parameters.Add("@jobcode", cmbjobcode.Text);
                    oleDbCommand1.Parameters.Add("@jobpk", cmbjobcode.SelectedValue);
                    oleDbCommand1.Parameters.Add("@sillabordercharges", int.Parse(txtsilabordercharges.Text));
                    oleDbCommand1.Parameters.Add("@battabordercharges", int.Parse(txtbattacharges.Text));
                    oleDbCommand1.Parameters.Add("@driverpayment", int.Parse(txtdriverpayment.Text));
                    oleDbCommand1.Parameters.Add("@rent", int.Parse(txtrent.Text));
                    oleDbCommand1.Parameters.Add("@extra", int.Parse(txtextra.Text));
                    oleDbCommand1.Parameters.Add("@total", int.Parse(txttotal.Text));
                    oleDbCommand1.Parameters.Add("@discount", int.Parse(txtdiscount.Text));
                    oleDbCommand1.Parameters.Add("@remark", txtremark.Text);
                    oleDbCommand1.Parameters.Add("@amount", int.Parse(txtamount.Text));
                    oleDbCommand1.CommandType = CommandType.Text;
                    oleDbCommand1.ExecuteNonQuery();
                    oleDbConnection1.Close();
                    MessageBox.Show(txtquotationno.Text);
    
                }
            }


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

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