首页 > 解决方案 > c# 不允许 idb2 连接

问题描述

我与 AS400 的连接有点问题。我使用的是 c#。当我想在表上执行插入 sql 语句时,它会弹出此消息

SystemInvalidOperationException :此操作无法成功,因为 IBM.Data.DB2.iSeries.iDB2Command.verifyConnection() 不允许连接;在 IBM.Data.DB2.iSeries.iDB2Command.ExecuteNonQuery();

这是我对连接字符串的定义

    public static string userID;
    public static string passwd;
    public static string system;
    public string query;
    public iDB2Connection conn = new iDB2Connection("DataSource=" + system + ";UserID=" + userID + ";Password=" + passwd + ";DataCompression=true;");

以及包含插入语句的代码

       public  void insert(Programs prog, int nbfiche)
        {
        //conn.Open();
        try
        {


            string sqlQuery = "INSERT INTO DIIAB.FICDET(MTPRO,MTFICH,MTPGM,MTNSRC,MTLSRC,MTTYP,MTOBJT) VALUES('" + Progiciel + "','" + nbfiche + "','" + prog.program_name +
                "','" + prog.source_program + "','" + LIB + "','" + prog.element_type + "','" + prog.program_type + "')";


            iDB2Command iDB2Command = conn.CreateCommand();
            iDB2Command.CommandText = sqlQuery;
            iDB2Command.ExecuteNonQuery();
            sqlQuery = "select MTFICH from DIIAB.FICDET where MTFICH='" + nbfiche + "'";
            iDB2Command command = conn.CreateCommand();
            command.CommandText = sqlQuery;
            iDB2DataReader reader = command.ExecuteReader();
            while (reader.Read())
            {

                if (reader[0].ToString().Contains(nbfiche.ToString()))
                {
                    System.Windows.MessageBox.Show("Un programme à été rajouté à la fiche.");
                }



            }

            System.Windows.MessageBox.Show("Les programmes ont été rajouté à la fiche", "Information");


        }
        catch (Exception e)
        {
            System.Windows.MessageBox.Show(e.ToString());
        }

    }

以及调用带有参数的方法插入的代码

         edit.userID = userID;
                            edit.passwd = passwd;
                            edit.system = system;
                            edit editeur = new edit();

                            editeur.nbfiche = Convert.ToInt32(daoficnbr.fICNBR.nb_fiche);
                            editeur.fiche_status = Statuss.Text;
                            editeur.Progiciel = PRO.Text;
                            editeur.getpgm(arcad.lib,daoficnbr.fICNBR.nb_fiche);
                            foreach (Programs p in editeur.content)
                            {
                                editeur.insert(p, editeur.nbfiche);
                            }

可以帮我吗,我已经坚持了两天了

标签: c#db2ibm-midrange

解决方案


解决方案是确保连接字符串以分号终止,并且 conn.Open() 在运行命令或查询之前成功完成。


推荐阅读