DataReader中处理NULL这是我通过DataReader从sql中读取数据的代码。当表中有NULL时会报错。如何处理?我试过c.ActualWeight=dr[0]asfloat???默认(浮动);问题在于它成功处理了NULL,但结果始终为0,即使sql数据库在该字段中的值为1。{列表LC=新列表();stringConString="datasource=DELL\SQLSERVER1;InitialCatalog=Camo;IntegratedSecurity=True";SqlConnectioncon=newSqlConnection(ConString);SqlCommandcmd=newSqlCommand("从Inventory_Connector中选择前17个*",con);con.Open();SqlDataReaderdr=cmd.ExecuteReader();while(dr.Read()){c.ActualWeight=float.Parse(dr[0].ToString().Trim());}LC.Add(c);}博士关闭();con.Close();退回信用证;您可以使用SqlDataReader.IsDBNull在数据读取器中检查空值。C#null和DBNull是不同的。c.ActualWeight=dr.IsDBNull(0)?默认(浮动):float.Parse(dr[0].ToString().Trim());你可以试试这个if(dr.IsDBNull(0))c。实际重量=默认(浮动);否则c.ActualWeight=float.Parse(dr[0].ToString().Trim());c.ActualWeight=(dr[0]!=DBNull.Value)?float.Parse(dr[0].ToString().Trim()):default(float)使用DBNull.Value检查空值。以上就是C#学习教程:HandlingNULLinDatareader全部内容分享。如果对你有用,需要进一步了解C#学习教程,希望大家多多关注。本文收集自网络,不代表立场。如涉及侵权,请点击右边联系管理员删除。如需转载请注明出处:
