C# 如何使Sqlcommand接受空值
我试图从数据库中获取gridview中的数据,以在单击时显示在文本框中,这对于没有空数据的行来说效果很好,尽管由于我的int列有一些空值,我的GetInt32方法不断返回“数据为空。不能对空值调用此方法或属性。” 有没有一种简单的方法来解决这个问题?我是否用另一个方法替换GetInt32?如果可能的话,我希望null数据在文本框中显示为空/空白。这是我的代码,如果你有任何建议,谢谢C# 如何使Sqlcommand接受空值,c#,sqlcommand,C#,Sqlcommand,我试图从数据库中获取gridview中的数据,以在单击时显示在文本框中,这对于没有空数据的行来说效果很好,尽管由于我的int列有一些空值,我的GetInt32方法不断返回“数据为空。不能对空值调用此方法或属性。” 有没有一种简单的方法来解决这个问题?我是否用另一个方法替换GetInt32?如果可能的话,我希望null数据在文本框中显示为空/空白。这是我的代码,如果你有任何建议,谢谢 public ArrayList GetAllPersonnel(int WorkerID) {
public ArrayList GetAllPersonnel(int WorkerID) {
using (var connection = new SqlConnection(connectionString)) {
connection.Open();
String query = "Select * FROM Personnel WHERE WorkerID = " + WorkerID;
using (var command = new SqlCommand(query, connection)) {
var reader = command.ExecuteReader();
var list = new ArrayList();
while (reader.Read()) {
String firstname = reader.GetString(1);
String lastname = reader.GetString(2);
String occupation = reader.GetString(3);
String deployment = reader.GetString(4);
int disasterid = reader.GetInt32(5);
String location = reader.GetString(6);
int deployedhours = reader.GetInt32(7);
int resthours = reader.GetInt32(8);
list.Add(firstname);
list.Add(lastname);
list.Add(occupation);
list.Add(deployment);
list.Add(disasterid);
list.Add(location);
list.Add(deployedhours);
list.Add(resthours);
}
connection.Close();
reader.Close();
return list;
}
}
}
您应该使用SqlDataReader的方法
int resthours = (!reader.IsDBNull(8) ? reader.GetInt32(8) : 0);
或者,更直接地说
list.Add((!reader.IsDBNull(8) ? reader.GetInt32(8).ToString(): string.Empty));
话虽如此,我注意到您使用字符串连接来构建sql命令文本以检索记录。请不要那样做。这是非常危险的,可能导致死亡
好的,你实际上是说你显示的所有东西都应该是一个
字符串
类型,这很好,我只是想指出这一点,因为你说你希望整数显示为空字符串。那么这个代码呢
String firstname = reader.GetString(1);
String lastname = reader.GetString(2);
String occupation = reader.GetString(3);
String deployment = reader.GetString(4);
String disasterid = reader.IsDBNull(5) ? string.Empty : reader.GetString(5);
String location = reader.GetString(6);
String deployedhours = reader.IsDBNull(7) ? string.Empty : reader.GetString(7);
String resthours = reader.IsDBNull(8) ? string.Empty : reader.GetString(8);
list.Add(firstname);
list.Add(lastname);
list.Add(occupation);
list.Add(deployment);
list.Add(disasterid);
list.Add(location);
list.Add(deployedhours);
list.Add(resthours);
现在,我之所以要将所有内容作为字符串使用,是因为
int
的默认值是0
,这不符合空文本框的要求。您至少有两种方法来解决这个问题
- 修改sql以选择零或任何您认为合适的值来代替空值。这将确保整型列中始终有一个整数值。可以通过以下方式完成 从xxx中选择ISNULL(ColumnName,0)作为ColumnName
- 始终从读取器获取对象并检查它是否为null。如果为空,则用合适的值替换它
读取器
中的字段值是多少?因为我们可以走另一条路。@NickV,太棒了!请随意编辑答案,并用您的更改更新代码。
String firstname = reader.GetString(1);
String lastname = reader.GetString(2);
String occupation = reader.GetString(3);
String deployment = reader.GetString(4);
String disasterid = reader.IsDBNull(5) ? string.Empty : reader.GetString(5);
String location = reader.GetString(6);
String deployedhours = reader.IsDBNull(7) ? string.Empty : reader.GetString(7);
String resthours = reader.IsDBNull(8) ? string.Empty : reader.GetString(8);
list.Add(firstname);
list.Add(lastname);
list.Add(occupation);
list.Add(deployment);
list.Add(disasterid);
list.Add(location);
list.Add(deployedhours);
list.Add(resthours);