Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在C#中将数据库对象转换为int?_C#_Database_Type Conversion - Fatal编程技术网

如何在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

如何在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 : (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"]);