开发者

How do I convert a database object to int in C#?

How do I convert a database object to int in C#?

int uid = Convert.ToInt32(dt.Rows[0开发者_如何学编程]["userid"].ToString());


You shouldn't convert the object to a string before converting to the int.

int uid = Convert.ToInt32(dt.Rows[0]["userid"]);

If you need to convert a string to an int then use;

int uid = int.Parse("1");


Use Null Check before Converting to integer.

DataRow row=dt.Rows[0];
int uid = row.IsNull("userid") ? 0 : (Convert.ToInt32(dt.Rows[0]["userid"]); 


Int32.Parse(...) ... - your ToString() method

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜