English 中文(简体)
C# “无效操作例外 : 执行不询问” 例外
原标题:C# "InvalidOperationException : ExecuteNonQuery" Exception

当我试图通过我创建的软件将一些数据保存到我的 sql (表达式) 时, 我有一个错误 。

An unhandled exception of type System.InvalidOperationException occurred in System.Data.dll. Additional information: ExecuteNonQuery: Connection property has not been initialized.

这是我的密码:

    private void save_medicine_Click(object sender, EventArgs e)
    {
        cn.Open();

        if (nameBox.Text != "" & companyBox.Text != "" & categoryBox.Text != "" & 
           amountBox.Text != "" & restockBox.Text != "" & buyBox.Text != "" & 
           sellBox.Text != "" & qualityComboBox.Text !=""  )
        {
            cmd.CommandText = "insert into drug_info (name,company_id,category,
                               expiration_date,stock_level,
                               critical_level_drug,cost_buy,cost_sell,drug_quality) 
                               values ( "+nameBox.Text+" , "+companyBox.Text+" , "+
                               categoryBox.Text+" , "+dateTimePicker1+" , "+amountBox+" , "
                                +restockBox+" , "+buyBox+" , "+sellBox+" , "
                                +qualityComboBox+" )";
            cmd.ExecuteNonQuery();
            cmd.Clone();

        }
    }
}
最佳回答

rMedociece_reg_load中,您有一个等级变量 cn 和本地变量 cn 。这似乎不对。

错误仅指您在指定连接之前试图使用 cmd

问题回答

首先:使用参数。 您可以阅读有关这些参数的 < a href=" http://www.google.com/ url?sa=t&rct=j&q=sqlcommander%20paraters&source=web&cd=1 &ved=0CGAQFAAA&url=http://msdn.microsoft.com/ en-us/library/ systems.data.sqlclient.sqlcorder. parameters.aspx&ei=Fw_CT_P9JobU-gaao-Cjg&usg=AFQjCNHLwGKRVFDT4ffzt5FMPDg&sig2=KwBAWM5T5T5JOIF8TDYQ; rel=“ un fol fol fol fol fol fol ref@he>

另一件事是这条线:

cmd.Clone();

我敢打赌,你想关闭指挥系统,但无法关闭一个斯克尔司令部,把线移开

您也应该用其它答案中提到的两个连接来清理这里的烂摊子。

像这样试试

     if (nameBox.Text != "" & companyBox.Text != "" & categoryBox.Text != "" &  amountBox.Text != "" & restockBox.Text != "" & buyBox.Text != "" & sellBox.Text != "" & qualityComboBox.Text !=""  )
    {

        string sql = "insert into drug_info (name,company_id,category,expiration_date,stock_level,critical_level_drug,cost_buy,cost_sell,drug_quality) values ( "+nameBox.Text+" , "+companyBox.Text+" , "+categoryBox.Text+" , "+dateTimePicker1+" , "+amountBox+" , "+restockBox+" , "+buyBox+" , "+sellBox+" , "+qualityComboBox+" )";
        SqlCommand cmd = new SqlCommand(sql,cn);
        cn.Open();
        cmd.ExecuteNonQuery();
        cn.Close();
        MessageBox.Show("Τα στοιχεία καταχωρήθηκαν στη Βάση");
    }

。 altho I don't know why value like "amountBox" 和 "buyBox" 等值为什么没有给它们贴上. text 。 我不知道这些值来自何方, 但是如果它们是文本框, 您可能需要添加. text 。 如果它们是文本框。 如果是的话, 您会尝试在数据库中插入它们, 这样您就会得到错误, 因为您无法在数据库中插入某种“ Textbox” 类型的东西 。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...