开发者

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

开发者 https://www.devze.com 2023-02-19 05:20 出处:网络
How do I convert a database object to int in C#? int uid = Convert.ToInt32(dt.Rows[0开发者_如何学编程][\"userid\"].ToString());

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

精彩评论

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