【问题标题】:Connection not open error Visual Studio c# OleDB连接未打开错误 Visual Studio c# OleDB
【发布时间】:2019-12-27 01:22:10
【问题描述】:

我想将新的 ID-Machine(不是自动的)、新机器的名称并选择该类型机器所属的区域(Bereich)放入一个列表中。 我尝试将 ID 号(如在数据库访问中)字符串更改为 ID 以获取 INSERT INTO 查询中的区域名称。但是我不知道为什么VS显示我没有打开连接(ExecuteNonQuery需要一个打开且可用的连接。连接的当前状态是关闭的)...如果有人可以帮助我,那就太好了。

public void bttnSpeichern_Click_1(System.Object sender, EventArgs e)
        {
            Module.con.Open();

            int geraetid = (int)(Conversion.Val(lblGeraetid.Text));
            int bereichid = (int)(Conversion.Val(lblBereichID.Text));

            if (lblGeraetid.Text == "MaschineID" || lblGeraetid.Text == null || txtGeraetName.Text == null || lblGeraetid.Text == "BereichID" || lblBereichID.Text == null)
            {
                Interaction.MsgBox("Bitte füllen Sie die Faldern aus", Constants.vbInformation, "Hinweis");
            }
            else
            {
                string b = System.Convert.ToString(Interaction.MsgBox("Möchten Sie die Eingaben bestätigen?", (int)Constants.vbQuestion + Constants.vbYesNo, "Anlegen"));
                if ( b == Constants.vbYes.ToString())
                {
                    Module.con.Open();
                    OleDbCommand geraete_anlegen = new OleDbCommand("INSERT INTO tblMaschine(MaschineID,Maschine,BereichID) VALUES (\'"+
                     lblGeraetid.Text + "\',\'"+
                     txtGeraetName.Text + "\',\'" +
                     lblBereichID.Text + "\',\'", Module.con);
                    geraete_anlegen.ExecuteNonQuery();

                    Interaction.MsgBox("Gerät wurde erfolgreich angelegt!", Constants.vbInformation, "Neues Gerät");
                    //clear_text();
                    Module.con.Close();
                    display_geraete();

                }
            }



        }

【问题讨论】:

  • 能把Module类相关的代码放上来吗?
  • 不要像那样打开你的连接。将这些一次性物品放入 using-block 中。使用参数避免sql注入和格式错误。
  • @萨曼:使用系统;使用 System.Collections.Generic;使用 System.Linq;使用 System.Text;使用 System.Threading.Tasks;使用 System.Data.OleDb; namespace Geraetverleich { public class Module { public static OleDbConnection con = new OleDbConnection("Provider=MICROSOFT.ACE.OLEDB.12.0; Data Source=|DataDirectory|/Geraetverleih.accdb"); } }
  • @Lars,但这是在哪里打开的?我想我用“Module.con.Close(); 关闭它吗?
  • 你没有关闭它if (lblGeraetid.Text == "MaschineID" ...

标签: c# winforms oledbconnection executenonquery


【解决方案1】:

我看到的第一个问题是您打开了两次连接。我评论了一个并在它旁边放了一条评论。

我不确定模块包含什么,因此我编写了一个独立的解决方案来解决您可以从中获取的问题。

我将连接放在 using 子句中,这始终是一个好习惯,因此您不必担心关闭它。

public void bttnSpeichern_Click_1(System.Object sender, EventArgs e)
{
    //Module.con.Open(); < -- Not needed it is done below

    int geraetid = (int)(Conversion.Val(lblGeraetid.Text));
    int bereichid = (int)(Conversion.Val(lblBereichID.Text));

    if (lblGeraetid.Text == "MaschineID" || lblGeraetid.Text == null || txtGeraetName.Text == null || lblGeraetid.Text == "BereichID" || lblBereichID.Text == null)
    {
        Interaction.MsgBox("Bitte füllen Sie die Faldern aus", Constants.vbInformation, "Hinweis");
    }
    else
    {
        string b = System.Convert.ToString(Interaction.MsgBox("Möchten Sie die Eingaben bestätigen?", (int)Constants.vbQuestion + Constants.vbYesNo, "Anlegen"));
        if ( b == Constants.vbYes.ToString())
        {
            string geraete_anlegen = new OleDbCommand("INSERT INTO tblMaschine(MaschineID,Maschine,BereichID) VALUES (\'"+
             lblGeraetid.Text + "\',\'"+
             txtGeraetName.Text + "\',\'" +
             lblBereichID.Text + "\',\'", Module.con);
            using (OleDbConnection connection = new OleDbConnection(connectionString)) //<-You need to supply a connection string
            {
                connection.Open();
                OleDbCommand command = new OleDbCommand(geraete_anlegen, connection);
                command.ExecuteNonQuery();
            }

            Interaction.MsgBox("Gerät wurde erfolgreich angelegt!", Constants.vbInformation, "Neues Gerät");
            //clear_text();
            display_geraete();

        }
    }

}

【讨论】:

  • 您能谈谈您的解决方案吗?刚刚打开了另一个连接!
  • @SamanGholami,我已经尝试过:public static OleDbConnection ConnectTo() { //1. Objekt Verbindung erstellen OleDbConnection Verbindung = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+ Application.StartupPath +"\\Geraetverleih.accdb");返回 Verbindung;和查询中的连接替换了模块类的使用,但我得到了同样的错误。
  • @Edney,它现在可以工作了我还有另一个错误,但它工作了,谢谢。
猜你喜欢
  • 2013-05-17
  • 1970-01-01
  • 1970-01-01
  • 2015-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-04
  • 1970-01-01
相关资源
最近更新 更多