我試圖從數據庫中獲取數據在GridView中顯示在文本框中單擊時,它適用於沒有空數據的行,但由於我的int列有一些空值我的GetInt32方法不斷返回「數據爲空。此方法或屬性不能在空值上調用。」如何使Sqlcommand接受空值
有沒有簡單的方法來解決或解決此問題?我用另一種方法替換GetInt32嗎?如果可能的話,我希望空白的數據在文本框中顯示空白/空白。這是我的代碼,如果您有任何建議,謝謝。
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;
}
}
}
歡迎來到堆棧溢出,未來我。 – 2013-04-29 13:07:40