简体   繁体   中英

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

如何在C#中将数据库对象转换为int?

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(...)... - 你的ToString()方法

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM