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

问题描述

我试图通过参数化方法将值插入到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?

推荐答案


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


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

10-22 13:14