SqlDataReader显示空列,直到出错。 [英] SqlDataReader showing Empty columns until after error.

查看:88
本文介绍了SqlDataReader显示空列,直到出错。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难过。我使用@ ID = 18对数据库运行程序Batch_Select

并且我得到了预期的数据。

当它加载到SqlDataReader中时,它会以某种方式混乱。 br />
最初,在reader.Read()之后,它有一行包含13个数据列,

但它们都是空的。所以,我的GetInt()函数抛出一个错误。当

跳转到catch()时,读者的列会显示正确的数据。

是什么给出的?


我的变量手表中有reader._data [0](ID列)。

它应该是一个Int32并且包含值18.

读者之后.Read()它有_type = Empty;值=;

下一行,int tid,抛出错误{指定的强制转换不是

有效。}并跳转到catch()

但是,在catch()之后,它有_type = Int32;价值= 18;


---------------------------------- -------------------------------------------------- ----------------------

SqlConnection con = new SqlConnection(connectionString);

SqlCommand cmd = new SqlCommand(" Batch_Select",con);

cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.Add(new SqlParameter(" @ ID", SqlDbType.Int));

cmd.Parameters [" @ ID"]。值= 18;

SqlDataReader阅读器;

try

{

con.Open();

reader = cmd.ExecuteReader();


if(reader.HasRows)

{

reader.Read();

int tid = GetInt(reader," ID");

int tpartid = GetInt(reader," PartID");

string tpartnum = GetString(reader," Part#");

string tpartname = GetString(reader," PartName");

string tdesc = GetString(reader," Description");

string trev = GetString(reader," Revision");

string tserial = GetString(reader," SerialNumber");

int tqty = GetInt(读者,数量);

string ttdr = GetString(reader," TDR");

string trm = GetString(reader," RM" );

string trmlot = GetString(reader," RMlot");

int tstatus = GetInt(reader," StatusID");

DateTime tdate = GetDate(reader," DateBorn");

}

else

{

抛出新的ApplicationException(" Retrieval Error:< br />

无法加载Part:" + id);

}

}

catch(异常错误)

{

抛出新的ApplicationException(" Data Error:< br />" +

err.Message);

}

终于

{

con.Close();

}

--------- -------------------------------------------------- -----------------------------------------------


我肯定有人会问,所以这里是GetInt()


--------- -------------------------------------------------- -----------------------------------------------

public static int GetInt(IDataReader reader,string columnName)

{

int columnNum = reader.GetOrdinal(columnName);

if(reader.IsDBNull(columnNum))

{

返回0;

}

else

{

return(int)reader [columnName];

}

}

-------------------------------------------------- ----------- ---------------------------------------------


这个完全相同的代码片段格式在另一个存储过程的另一个

函数中正常工作,因此我的困惑。但是,此代码在另一个类中是
。我正在试图弄清楚班级里有什么东西可以干扰。

解决方案

< blockquote>完全抛出的错误是什么?这是你的关键部分

没有告诉我们。


" lithoman" < li ****** @ gmail.com写信息

新闻:11 ********************* @ j44g2000cwa。 googlegro ups.com ...


我很难过这里。我使用@ ID = 18对数据库运行程序Batch_Select

并且我得到了预期的数据。

当它加载到SqlDataReader中时,它会以某种方式混乱。 br />
最初,在reader.Read()之后,它有一行包含13个数据列,

但它们都是空的。所以,我的GetInt()函数抛出一个错误。当

跳转到catch()时,读者的列会显示正确的数据。

是什么给出的?


我的变量手表中有reader._data [0](ID列)。

它应该是一个Int32并且包含值18.

读者之后.Read()它有_type = Empty;值=;

下一行,int tid,抛出错误{指定的强制转换不是

有效。}并跳转到catch()

但是,在catch()之后,它有_type = Int32;价值= 18;


---------------------------------- -------------------------------------------------- ----------------------

SqlConnection con = new SqlConnection(connectionString);

SqlCommand cmd = new SqlCommand(" Batch_Select",con);

cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.Add(new SqlParameter(" @ ID", SqlDbType.Int));

cmd.Parameters [" @ ID"]。值= 18;

SqlDataReader阅读器;

try

{

con.Open();

reader = cmd.ExecuteReader();


if(reader.HasRows)

{

reader.Read();

int tid = GetInt(reader," ID");

int tpartid = GetInt(reader," PartID");

string tpartnum = GetString(reader," P art#");

string tpartname = GetString(reader," PartName");

string tdesc = GetString(reader," Description");

string trev = GetString(reader," Revision");

string tserial = GetString(reader," SerialNumber");

int tqty = GetInt(reader,Quantity);

string ttdr = GetString(reader,TDR);

string trm = GetString(reader," RM" ;);

string trmlot = GetString(reader," RMlot");

int tstatus = GetInt(reader," StatusID");

DateTime tdate = GetDate(reader," DateBorn");

}

else

{

抛出新的ApplicationException(" Retrieval Error:< br />

Unab le to load Part: + id);

}

}

catch(异常错误)

{

抛出新的ApplicationException(" Data Error:< br />" +

err.Message);

}

终于

{

con.Close();

}

--------- -------------------------------------------------- -----------------------------------------------


我肯定有人会问,所以这里是GetInt()


--------- -------------------------------------------------- -----------------------------------------------

public static int GetInt(IDataReader reader,string columnName)

{

int columnNum = reader.GetOrdinal(columnName);

if(reader.IsDBNull(columnNum))

{

返回0;

}

else

{

return(int)reader [columnName];

}

}

--------- -------------------------------------------------- -----------------------------------------------


这个完全相同的代码片段格式在另一个存储过程的另一个

函数中正常工作,因此我的困惑。但是,此代码在另一个类中是
。我正在试图弄清楚班级里是否有任何可能干扰的东西。



Marina Levit [MVP]" < so ***** @ nospam.comwrote in message

news:eP ************* @ TK2MSFTNGP06.phx.gbl ...


这是你没有告诉我们的关键部分。



这永远不会让我惊讶......!




我会尝试不同的语法。


reader.Read();

int tid = GetInt(reader," ID" );




reader.Read();

object o = reader.GetValue(0); // 0是序数列值

int tid = reader.GetInt32(0);


为什么要使用对象?如果类型不是你所期望的那样,你可以

找出那种方式。

不要用它来生产代码(.GetValue那个是... ....直到你...

搞定了问题。


" lithoman" < li ****** @ gmail.com写信息

新闻:11 ********************* @ j44g2000cwa。 googlegro ups.com ...


我很难过这里。我使用@ ID = 18对数据库运行程序Batch_Select

并且我得到了预期的数据。

当它加载到SqlDataReader中时,它会以某种方式混乱。 br />
最初,在reader.Read()之后,它有一行包含13个数据列,

但它们都是空的。所以,我的GetInt()函数抛出一个错误。当

跳转到catch()时,读者的列会显示正确的数据。

是什么给出的?


我的变量手表中有reader._data [0](ID列)。

它应该是一个Int32并且包含值18.

读者之后.Read()它有_type = Empty;值=;

下一行,int tid,抛出错误{指定的强制转换不是

有效。}并跳转到catch()

但是,在catch()之后,它有_type = Int32;价值= 18;


---------------------------------- ----------------------------------------



--------------------------------


SqlConnection con = new SqlConnection(connectionString);

SqlCommand cmd = new SqlCommand(" Batch_Select",con);

cmd.CommandType = CommandType.StoredProcedure ;

cmd.Parameters.Add(new SqlParameter(" @ @ ID",SqlDbType.Int));

cmd.Parameters [" @ ID"]。 = 18;

SqlDataReader阅读器;

尝试

{

con.Open();

reader = cmd.ExecuteReader();


if(reader.HasRows)

{

reader.Read ();

int tid = GetInt(reader," ID");

int tpartid = GetInt(reader," PartID");

string tpartnum = GetString(reader," Part#");

string tpartname = GetString(reader," PartName");

string tdesc = GetString(reader," Description");

string trev = GetString(reader," Revision");

string tserial = GetString(reader," SerialNumber");

int tqty = GetInt(reader," Quantity");

string ttdr = GetString(reader," TDR" ;);

string trm = GetString(reader," RM");

string trmlot = GetString(reader," RMlot");

int tstatus = GetInt(reader," StatusID");

DateTime tdate = GetDate(reader," DateBorn");

}

else

{

抛出新的ApplicationException(" Retrieval Error:< br />

无法加载Part:" + id);

}

}

catch(异常错误)

{

抛出新的ApplicationException(" Data Error:< br />" +

err.Message);

}

终于

{

con.Close();

}

--------- -------------------------------------------------- ---------------



------------------- -------------


>

我肯定会有人问,所以这里是GetInt()


----------------------------- ---------------------------------------------



--------------------------------


public static int GetInt(IDataReader reader,string columnName)

{

int columnNum = reader.GetOrdinal(columnName);

if(reader.IsDBNull(columnNum))

{

返回0;

}

否则

{

return(int)读者[columnName];

}

}

----------------------------------------------- ---------------------------



------- -------------------------


>

这个完全相同的代码片段格式在另一个存储过程的另一个

函数中正常工作,因此我感到困惑。但是,此代码在另一个类中是
。我正在试图弄清楚班上是否有任何可能会干扰的东西。



I''m stumped here. I run the procedure Batch_Select against the database
with @ID=18 and I get the expected data.
When it loads into a SqlDataReader, it gets messed up somehow.
Initially, after the reader.Read(), it has a row with 13 data columns,
but they''re all empty. So, my GetInt() function throws an error. When
it jumps to the catch(), reader''s columns then show the proper data.
What gives?

I have reader._data[0] (which is the ID column) in my variable Watch
and it should be an Int32 and contain the value 18.
After reader.Read() it has _type=Empty; Value=;
The next line, int tid, throws an error {"Specified cast is not
valid."} and jumps to catch()
After catch(), however, it has _type=Int32; Value=18;

----------------------------------------------------------------------------------------------------------
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand("Batch_Select", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@ID", SqlDbType.Int));
cmd.Parameters["@ID"].Value = 18;
SqlDataReader reader;
try
{
con.Open();
reader = cmd.ExecuteReader();

if (reader.HasRows)
{
reader.Read();
int tid = GetInt(reader, "ID");
int tpartid = GetInt(reader, "PartID");
string tpartnum = GetString(reader, "Part#");
string tpartname = GetString(reader, "PartName");
string tdesc = GetString(reader, "Description");
string trev = GetString(reader, "Revision");
string tserial = GetString(reader, "SerialNumber");
int tqty = GetInt(reader, "Quantity");
string ttdr = GetString(reader, "TDR");
string trm = GetString(reader, "RM");
string trmlot = GetString(reader, "RMlot");
int tstatus = GetInt(reader, "StatusID");
DateTime tdate = GetDate(reader, "DateBorn");
}
else
{
throw new ApplicationException("Retrieval Error:<br />
Unable to load Part: " + id);
}
}
catch (Exception err)
{
throw new ApplicationException("Data Error:<br />" +
err.Message);
}
finally
{
con.Close();
}
----------------------------------------------------------------------------------------------------------

I''m sure someone is going to ask, so here''s the GetInt()

----------------------------------------------------------------------------------------------------------
public static int GetInt(IDataReader reader, string columnName)
{
int columnNum = reader.GetOrdinal(columnName);
if (reader.IsDBNull(columnNum))
{
return 0;
}
else
{
return (int)reader[columnName];
}
}
----------------------------------------------------------------------------------------------------------

And this exact same code snippet format is working fine in another
function for another Stored Procedure, thus my confusion. This code is
in a different class, however. I''m trying to figure out if there is
anything in the class that could interfere.

解决方案

What is the error that gets thrown exactly? That''s a key part of this you
didn''t tell us.

"lithoman" <li******@gmail.comwrote in message
news:11*********************@j44g2000cwa.googlegro ups.com...

I''m stumped here. I run the procedure Batch_Select against the database
with @ID=18 and I get the expected data.
When it loads into a SqlDataReader, it gets messed up somehow.
Initially, after the reader.Read(), it has a row with 13 data columns,
but they''re all empty. So, my GetInt() function throws an error. When
it jumps to the catch(), reader''s columns then show the proper data.
What gives?

I have reader._data[0] (which is the ID column) in my variable Watch
and it should be an Int32 and contain the value 18.
After reader.Read() it has _type=Empty; Value=;
The next line, int tid, throws an error {"Specified cast is not
valid."} and jumps to catch()
After catch(), however, it has _type=Int32; Value=18;

----------------------------------------------------------------------------------------------------------
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand("Batch_Select", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@ID", SqlDbType.Int));
cmd.Parameters["@ID"].Value = 18;
SqlDataReader reader;
try
{
con.Open();
reader = cmd.ExecuteReader();

if (reader.HasRows)
{
reader.Read();
int tid = GetInt(reader, "ID");
int tpartid = GetInt(reader, "PartID");
string tpartnum = GetString(reader, "Part#");
string tpartname = GetString(reader, "PartName");
string tdesc = GetString(reader, "Description");
string trev = GetString(reader, "Revision");
string tserial = GetString(reader, "SerialNumber");
int tqty = GetInt(reader, "Quantity");
string ttdr = GetString(reader, "TDR");
string trm = GetString(reader, "RM");
string trmlot = GetString(reader, "RMlot");
int tstatus = GetInt(reader, "StatusID");
DateTime tdate = GetDate(reader, "DateBorn");
}
else
{
throw new ApplicationException("Retrieval Error:<br />
Unable to load Part: " + id);
}
}
catch (Exception err)
{
throw new ApplicationException("Data Error:<br />" +
err.Message);
}
finally
{
con.Close();
}
----------------------------------------------------------------------------------------------------------

I''m sure someone is going to ask, so here''s the GetInt()

----------------------------------------------------------------------------------------------------------
public static int GetInt(IDataReader reader, string columnName)
{
int columnNum = reader.GetOrdinal(columnName);
if (reader.IsDBNull(columnNum))
{
return 0;
}
else
{
return (int)reader[columnName];
}
}
----------------------------------------------------------------------------------------------------------

And this exact same code snippet format is working fine in another
function for another Stored Procedure, thus my confusion. This code is
in a different class, however. I''m trying to figure out if there is
anything in the class that could interfere.



"Marina Levit [MVP]" <so*****@nospam.comwrote in message
news:eP*************@TK2MSFTNGP06.phx.gbl...

That''s a key part of this you didn''t tell us.

That never ceases to amaze me...!



I would try a different syntax.

reader.Read();
int tid = GetInt(reader, "ID");



reader.Read();
object o = reader.GetValue(0); // 0 is the ordinal column value
int tid = reader.GetInt32(0);

Why use object? Just in case the type is not what youre expecting, you can
figure out the type that way.
Don''t use it for production code (the .GetValue that is) .... just til you
get the kinks out.


"lithoman" <li******@gmail.comwrote in message
news:11*********************@j44g2000cwa.googlegro ups.com...

I''m stumped here. I run the procedure Batch_Select against the database
with @ID=18 and I get the expected data.
When it loads into a SqlDataReader, it gets messed up somehow.
Initially, after the reader.Read(), it has a row with 13 data columns,
but they''re all empty. So, my GetInt() function throws an error. When
it jumps to the catch(), reader''s columns then show the proper data.
What gives?

I have reader._data[0] (which is the ID column) in my variable Watch
and it should be an Int32 and contain the value 18.
After reader.Read() it has _type=Empty; Value=;
The next line, int tid, throws an error {"Specified cast is not
valid."} and jumps to catch()
After catch(), however, it has _type=Int32; Value=18;

--------------------------------------------------------------------------

--------------------------------

SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand("Batch_Select", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@ID", SqlDbType.Int));
cmd.Parameters["@ID"].Value = 18;
SqlDataReader reader;
try
{
con.Open();
reader = cmd.ExecuteReader();

if (reader.HasRows)
{
reader.Read();
int tid = GetInt(reader, "ID");
int tpartid = GetInt(reader, "PartID");
string tpartnum = GetString(reader, "Part#");
string tpartname = GetString(reader, "PartName");
string tdesc = GetString(reader, "Description");
string trev = GetString(reader, "Revision");
string tserial = GetString(reader, "SerialNumber");
int tqty = GetInt(reader, "Quantity");
string ttdr = GetString(reader, "TDR");
string trm = GetString(reader, "RM");
string trmlot = GetString(reader, "RMlot");
int tstatus = GetInt(reader, "StatusID");
DateTime tdate = GetDate(reader, "DateBorn");
}
else
{
throw new ApplicationException("Retrieval Error:<br />
Unable to load Part: " + id);
}
}
catch (Exception err)
{
throw new ApplicationException("Data Error:<br />" +
err.Message);
}
finally
{
con.Close();
}
--------------------------------------------------------------------------

--------------------------------

>
I''m sure someone is going to ask, so here''s the GetInt()

--------------------------------------------------------------------------

--------------------------------

public static int GetInt(IDataReader reader, string columnName)
{
int columnNum = reader.GetOrdinal(columnName);
if (reader.IsDBNull(columnNum))
{
return 0;
}
else
{
return (int)reader[columnName];
}
}
--------------------------------------------------------------------------

--------------------------------

>
And this exact same code snippet format is working fine in another
function for another Stored Procedure, thus my confusion. This code is
in a different class, however. I''m trying to figure out if there is
anything in the class that could interfere.



这篇关于SqlDataReader显示空列,直到出错。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆