如何使用从SqlDataReader对象读取的整数值? [英] How do I work with an integer value read from SqlDataReader object?

查看:41
本文介绍了如何使用从SqlDataReader对象读取的整数值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我有一个SqlDataReader对象:

 SqlDataReaderEx sdrEx = new SqlDataReaderEx(mConnectionString); 

我创建了一个查询字符串,它能够从SQL数据库中读取字符串值,我可以将这些值精确分配给字符串变量。 例如,我可以使用"firstname"。列字段(这是varchar数据类型),并将
分配给字符串变量,如下所示:


 string mStudentFirst; 
mStudentFirst = sdrEx.String(" firstname");

但是,如果我尝试使用来自a的smallint值SQL数据库,我无法让它工作。 在下面的代码中,"Typeclass"表示"Typeclass"。 SQL数据库的列是"smallint"。数据类型:


 int mTypeclass; 
mTypeclass = Int32.Parse(sdrEx.String ("Typeclass"));

我查看了visual studio中sdrEx(Sql datareader)对象的可用方法,我没有看到任何其他内容这可以帮助我。 我尝试使用"长"和"toString",但即使在尝试
运行代码之前,这些也会立即出错。  "sdrEx.String"上面的代码在运行之前不会立即抛出任何错误,但在实际运行时,我在Web浏览器中出现此错误(下面只显示部分错误):


源错误:
$


在执行当前Web请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪来识别有关异常的起源和位置的信息。



堆栈跟踪: 


[FormatException:输入字符串的格式不正确。]

    System.Number.StringToNumber(String str,NumberStyles options,NumberBuffer& number,NumberFormatInfo info,Boolean parseDecimal)+14198648

    System.Number.ParseInt32(String s,NumberStyles style,NumberFormatInfo info)+177


为什么上述代码不起作用?    sdrEx.String应该作为字符串接收列,然后我尝试将该字符串解析为Int32,并将其分配给mTypeclass整数变量。 这有什么问题? 可能,
可能是sdrEx.String可能只期望varchar / char类型的列数据,但我没有看到任何其他我可以使用的SqlDataReader方法。


有人可以帮我纠正我有问题的代码吗?


谢谢!





 [FormatException:输入字符串的格式不正确。] 
System.Number.StringToNumber(String str, NumberStyles选项,NumberBuffer& number,NumberFormatInfo info,Boolean parseDecimal)+14198648
System.Number.ParseInt32(String s,NumberStyles style,NumberFormatInfo info)+177

解决方案

SQL数据库中的整数是.Net中的整数,无论​​其名称是什么。 


但是,在这个论坛中非ASP.Net问题e。 


尝试ASP.Net论坛。


http://forums.asp.net


Hello,

I have a SqlDataReader object:

SqlDataReaderEx sdrEx = new SqlDataReaderEx(mConnectionString);

and I created a query string, and it is able to read in string values fine from a SQL database, and I can assign those values fine to string variables.  For example, I can take a "firstname" column field (which is varchar datatype), and assign it to a string variable like below:

string mStudentFirst;
mStudentFirst = sdrEx.String("firstname");

However, if I try and work with a smallint value from a SQL database, I cannot get it to work.  In the below code, the "Typeclass" column of the SQL database is a "smallint" data type:

int mTypeclass;
mTypeclass = Int32.Parse(sdrEx.String("Typeclass"));

I've looked at the available methods of the sdrEx (Sql datareader) object in visual studio, and I don't see anything else that could help me.  I tried using "long" and "toString", but those give immediate errors even before attempting to run the code.  The "sdrEx.String" code I have above does not throw any immediate errors before I run it, but upon actual running, I get this error in my web browser (only partial error is shown below):

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace: 

[FormatException: Input string was not in a correct format.]
   System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) +14198648
   System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) +177

Why does the above code not work?   sdrEx.String should be taking in the column as a string, and then I try to parse that string into an Int32, and assign it to the mTypeclass integer variable.  What's the issue here?  Potentially, it could be that sdrEx.String may be expecting only varchar/char type column data, but I don't see any other SqlDataReader methods that I could use instead.

Can someone please assist me in correcting my problematic code?

Thanks!


[FormatException: Input string was not in a correct format.]
   System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) +14198648
   System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) +177

解决方案

An integer from a SQL Database is an integer in .Net, whatever other name it has. 

However, in this forum are non ASP.Net question done. 

Try for that the ASP.Net forums.

http://forums.asp.net


这篇关于如何使用从SqlDataReader对象读取的整数值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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