在 SQL Server 2008 R2 中使用记录集值
Using recordset values in SQL Server 2008 R2
我使用 Dynamics AX 2012 和 C# 中的业务连接器通过 odbc 方法检索数据。
我正在使用 SQL Server 2008 R2(版本 10.50.2500 - 不是 AX 数据库)。
代码如下所示:
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.add(0)... )。
似乎只有 table-列失败,它们在数据库中的类型是:int,或者对于值为 0 的 bigint 类型。
我该怎么做才能使代码不会像描述的那样失败?
谢谢:)
从 odbc 填充数据可以通过多种方式完成,而不仅仅是 odbcDataReader(这种方式不方便,因为需要逐条获取记录)。
当使用 OdbcDataAdapter class 时,喜欢 link https://msdn.microsoft.com/en-us/library/system.data.odbc.odbcdataadapter%28v=vs.110%29.aspx
以下代码也可能有所帮助:
...
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 类型。
我使用 Dynamics AX 2012 和 C# 中的业务连接器通过 odbc 方法检索数据。
我正在使用 SQL Server 2008 R2(版本 10.50.2500 - 不是 AX 数据库)。
代码如下所示:
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.add(0)... )。
似乎只有 table-列失败,它们在数据库中的类型是:int,或者对于值为 0 的 bigint 类型。
我该怎么做才能使代码不会像描述的那样失败?
谢谢:)
从 odbc 填充数据可以通过多种方式完成,而不仅仅是 odbcDataReader(这种方式不方便,因为需要逐条获取记录)。
当使用 OdbcDataAdapter class 时,喜欢 link https://msdn.microsoft.com/en-us/library/system.data.odbc.odbcdataadapter%28v=vs.110%29.aspx
以下代码也可能有所帮助:
...
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 类型。