C# 在SQL Server 2008 R2中使用记录集值
我在C中使用Dynamics AX 2012和business connector通过odbc方法检索数据 我使用的是SQL Server 2008 R2版本10.50.2500,而不是AX数据库 代码如下所示:C# 在SQL Server 2008 R2中使用记录集值,c#,sql-server,sql-server-2008-r2,axapta,dynamics-ax-2012,C#,Sql Server,Sql Server 2008 R2,Axapta,Dynamics Ax 2012,我在C中使用Dynamics AX 2012和business connector通过odbc方法检索数据 我使用的是SQL Server 2008 R2版本10.50.2500,而不是AX数据库 代码如下所示: using MIL = Miceoaodr.Dynamics.AX.ManagedInterop; ... namespae mynamespace { public class myclass { public static MIL.Session axSess
using MIL = Miceoaodr.Dynamics.AX.ManagedInterop;
...
namespae mynamespace
{
public class myclass
{
public static MIL.Session axSession = null;
...
public void test()
{
MIL.Container c;
OdbcDataReader r;
OdbcConnection conn;
OdbcCommand cmd;
object o;
conn = new OdbcConnection("my connection string");
conn.open();
cmd = new OdbcCommand("select * from mytable", conn);
r = cmd.ExecuteReader();
while (r.Read())
{
c.clear();
for (int i = 0; i < reader.FieldCount; i++)
{
o = reader.getValue(i);
c.Add(o); // **** fails sometimes
}
}
c = new MIL.Container();
c.add(0); // **** here is the problem. program halts without any **** warning!
}
}
}
带星号的行有时会失败c.add0
似乎只有表列的类型在db中为:int或类型为bigint且值为0时才失败
我该怎么做才能使代码不会像描述的那样失败
谢谢:odbc的升级数据可以通过多种方式完成,而不仅仅是odbcDataReader,这是一种不方便的方式,因为需要逐个记录地获取数据 使用OdbcDataAdapter类时,如链接 此外,以下代码可能会有所帮助:
...
OdbcDataAdapter d;
DataTable dt;
d = new OdbcDataAdapter(cmd);
d.Fill(dt);
...
foreach (DataRow row in dt.Rows)
{
foreach (DataColumn column in dt.Columns)
{ // fill the column header for the first time by column.ColumnName.
}
foreach (DataColumn column in dt.Columns)
{ // fill the colum values using row[column] combination.
}
}
无需将值从数据库类型转换为.net类型。您看到的实际错误是什么?我看不到任何错误。我在我的博客上看到,我想我应该在sqltype和.net类型之间进行转换,但我没有找到任何进行这种转换的类。谢谢:如果c.addo失败了一些时候,它应该很简单,把它周围的尝试捕捉,并找出它失败的原因?不,我知道为什么这是失败的。sql sqldbtype中的类型与.net类型不同,因此我需要对其进行转换。谢谢: