【发布时间】:2018-01-19 17:42:36
【问题描述】:
我正在从事 Ranorex 项目,但这不是 Ranorex 特定的问题。我需要打开一个到 AS400 DB2 数据库的连接,然后对其运行 SQL 查询。
我有成功打开连接并返回 SQL 查询结果的代码。
iDB2Connection cn = new iDB2Connection("Data Source=as400_DB;User ID=CLIENT;Password=CLIENT;Default Collection=XXXXXX;Naming=System");
cn.Open();
iDB2Command cmd = new iDB2Command("select count(*) from ABC_table where xxxx = 'WC' AND xxxx = 'L302328'", cn);
int count = Convert.ToInt32(cmd.ExecuteScalar());
Report.Log(ReportLevel.Info, "count", count.ToString());
cn.Close();
在代码中,第二种方法调用第一种方法打开连接,然后允许第二种方法运行SQL命令,但我得到以下错误:
无法隐式转换类型“IBM.Data.DB2.iSeries.iDB2DataReader” 到'System.Data.SqlClient.SqlDataReader'
在第一种方法中的“return new iDB2Connection(cn.ToString())”点。
public static class sQlHelper
{
private static SqlConnection sQlConnect()
{
iDB2Connection cn = new iDB2Connection("Data Source=as400_DB;User ID=CLIENT;Password=CLIENT;Default Collection=XXXXXX;Naming=System");
return new iDB2Connection(cn.ToString());
}
public static void validateResult()
{
var myConnection = sQlConnect();
myConnection.Open();
string sqlStatementForCheckHeaders = "select count(*) from ABC_table where xxxx = 'WC' AND xxxx = 'L302328'";
int count = Convert.ToInt32(cmd.ExecuteScalar());
SqlDataReader myReader = null;
iDB2Command myCommand = new iDB2Command(sqlStatementForCheckHeaders, myConnection);
myReader = myCommand.ExecuteReader();
while(myReader.Read())
{
Console.WriteLine(myReader["Column1"].ToString());
Console.WriteLine(myReader["Column2"].ToString());
}
myConnection.Close();
}
}
我无法确定如何解决问题,也无法找到有关错误本身的任何信息。我将不胜感激人们可以提供的任何支持/建议。
【问题讨论】:
-
你的方法 sQlConnect 有点多余。
标签: c# sql db2 ibm-midrange ranorex