如何在C#中将数据库对象转换为int?
如何在C#中将数据库对象转换为int如何在C#中将数据库对象转换为int?,c#,database,type-conversion,C#,Database,Type Conversion,如何在C#中将数据库对象转换为int 在转换为int之前,不应将对象转换为字符串 int uid = Convert.ToInt32(dt.Rows[0]["userid"]); 如果需要将字符串转换为int,则使用 int uid = int.Parse("1"); Int32.Parse(…) ... - 您的ToString()方法在转换为整数之前使用空检查 DataRow row=dt.Rows[0]; int uid = row.IsNull("userid") ? 0 : (Co
在转换为int之前,不应将对象转换为字符串
int uid = Convert.ToInt32(dt.Rows[0]["userid"]);
如果需要将字符串转换为int,则使用
int uid = int.Parse("1");
Int32.Parse(…)
... - 您的ToString()方法在转换为整数之前使用空检查
DataRow row=dt.Rows[0];
int uid = row.IsNull("userid") ? 0 : (Convert.ToInt32(dt.Rows[0]["userid"]);
数据库列的类型是什么?那代码会发生什么?(另外,如果要将其转换为int,为什么要将其转换为字符串?)。C#区分大小写。2.在代码前放四个空格,将其格式化为代码。对报价块使用
。页面上有更多信息。@martinho,干杯,我现在不在IDE前面测试代码。
DataRow row=dt.Rows[0];
int uid = row.IsNull("userid") ? 0 : (Convert.ToInt32(dt.Rows[0]["userid"]);