How do I convert a database object to int in C#?
如何在C#中将数据库对象转换为int?
int uid = Convert.ToInt32(dt.Rows[0]["userid"].ToString());
3 个解决方案
#1
6
You shouldn't convert the object to a string before converting to the int.
在转换为int之前,不应将对象转换为字符串。
int uid = Convert.ToInt32(dt.Rows[0]["userid"]);
If you need to convert a string to an int then use;
如果需要将字符串转换为int,则使用;
int uid = int.Parse("1");
#2
5
Use Null Check before Converting to integer.
在转换为整数之前使用Null Check。
DataRow row=dt.Rows[0];
int uid = row.IsNull("userid") ? 0 : (Convert.ToInt32(dt.Rows[0]["userid"]);
#3
1
Int32.Parse(...) ... - your ToString() method
Int32.Parse(...)... - 你的ToString()方法
#1
6
You shouldn't convert the object to a string before converting to the int.
在转换为int之前,不应将对象转换为字符串。
int uid = Convert.ToInt32(dt.Rows[0]["userid"]);
If you need to convert a string to an int then use;
如果需要将字符串转换为int,则使用;
int uid = int.Parse("1");
#2
5
Use Null Check before Converting to integer.
在转换为整数之前使用Null Check。
DataRow row=dt.Rows[0];
int uid = row.IsNull("userid") ? 0 : (Convert.ToInt32(dt.Rows[0]["userid"]);
#3
1
Int32.Parse(...) ... - your ToString() method
Int32.Parse(...)... - 你的ToString()方法