c# - 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);
}
可以帮我吗,我已经坚持了两天了
解决方案
解决方案是确保连接字符串以分号终止,并且 conn.Open() 在运行命令或查询之前成功完成。
推荐阅读
- postgresql - Postgres 数据库端口 5432。它是否连接到互联网?
- python - paho-mqtt-python 使用来自服务器的 TLS ca_certs 发布单个
- ruby-on-rails - Rails 作业因使用 mqtt 的 sidekiq 错误损坏管道而卡住
- mysql - 我是否正确计算了 MySQL 数据库的最大连接限制?
- scala - 如何将 ReactiveMongo 流与聚合一起使用?
- php - 尝试使用php在服务器(mamp)中上传文件
- asp.net-core - 将单角色管理升级为多角色管理
- python - 在python中重置字典不能从模块中工作
- jupyter-notebook - ipyvuetify 用户输入文本字段
- node.js - 将客户电子邮件传递给条带结帐