我需要一些幫助來理解我的錯誤。我想從表中讀取數據,但是我得到錯誤,例如「行/列沒有數據」。我不明白,因爲我實際上有行和列在那裏。的WinForms。謝謝!DataReader錯誤 - 「行/列沒有數據」 - 數據存在時?
//this is how i insert data into table, works fine
public void b1_Click(object sender, EventArgs e)
{
SqlCeCommand command = new SqlCeCommand("INSERT INTO tbl1(Name, LastName) VALUES (@Name, @LastName)", conn);
command.Parameters.AddWithValue("@Name", l1.Text);
command.ExecuteNonQuery();
}
//this is how i try to read data from the same table
public void b2_Click(object sender, EventArgs e)
{
SqlCeConnection conn = new SqlCeConnection(@"Data Source=C:test.sdf");
conn.Open();
SqlCeCommand command = new SqlCeCommand("SELECT * FROM tbl1", conn);
SqlCeDataReader reader = command.ExecuteReader();
//error here
string Name = reader.GetString(0);
label.Text = Name;
}
請務必包括*僅*的相關代碼下一次(繼續前進,現在去掉無關的代碼);方法定義也可以被刪除,因爲它不適用於問題。 – 2012-10-07 23:21:22