I've been trying to connect to a DB2 database on AS/400 for days. After I installed the IBM System i Access for Windows client I could create an ODBC data source from visual studio and when I click on "test connection" it is sucessful, However. executing a simple SELECT statement results in an infinite wait since database doesn't seem to respond to it: The code I'm using to connect and query is:
OdbcConnection conn = new OdbcConnection(@"Dsn=TEST1;Uid=myuser;Pwd=mypwd;DBQ=mydb2");
conn.Open();
try
{
string cmmTxt = query;
OdbcCommand cmd = new OdbcCommand(cmmTxt, conn);
OdbcDataAdapter da = new OdbcDataAdapter(cmd);
da.Fill(dset);
}
catch (Exception e)
{
Console.Write(e.StackTrace);
}
finally
{
conn.Close();
}
The line where the execution stops responding is "da.Fill(dset);". BTW I'm using visual studio 2010 and I don't see any error message, but the code never finishes "waiting" after that line. Do you have any ideas? Thanks in advance
Setting LONGDATACOMPAT flag as 1 in the connection string worked for me.
[C#]
OdbcConnection con =
new OdbcConnection("DSN=SAMPLE;UID=uid;PWD=mypwd;
LONGDATACOMPAT=1;");
see the full explanation in the URL below
I'm not sure exactly what the problem is, but can you try using a data reader and see if that gives you better results?
OdbcConnection conn = new OdbcConnection(@"Dsn=TEST1;Uid=myuser;Pwd=mypwd;DBQ=mydb2");
string SQL = "SELECT COUNT(*) FROM MyTable";
using (OdbcCommand com = new OdbcCommand(SQL, connection, null))
{
using (OdbcDataReader reader = com.ExecuteReader())
{
while (reader.Read())
{
var value = reader["MyColumn"];
}
}
}
Second, are you sure that your query will return in a reasonable amount of time? Do you have some tool which lets you run queries directly against the database (I'm not familiar with DB2, not sure if there is such a thing). Or is there a "profiler" that lets you "peek" at the database queries as they come in?
Since you don't show your query, I wonder if it's a very long-running query. How long did you wait for the query to return?
John
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.