![](/img/trans.png)
[英]InvalidOperationException when i try to retrieve a password from database, “Invalid attempt to read when no data is present.”
[英]I got exception when try to read data from database with the help of OleDb
您好,我正在嘗試使用OleDb連接數據庫,但是當我想從那里讀取數據並在那之后使用Read()命令時
cmd.Parameters.Add("@name", TextBox1.Text);
cmd.Parameters.Add("@password", TextBox2.Text);
cmd.ExecuteNonQuery();
System.Data.OleDb.OleDbDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
string istifadeciAd = (string)rdr.GetString(1);
string istifadeciParol = (string)rdr.GetString(2);
}
在String istifadeciAd和istifadeciParol中,由於IndexoutofRange而導致GetString錯誤。 但是我們不需要用列索引調用GetString嗎?
您的select語句中需要有2個以上的select列才能獲取rdr.GetString(2)
像下面這樣
select id, istifadeciAd, istifadeciParol from Table1 where name =? and password =?
請注意,索引是從零開始的
因此,如果您僅選擇istifadeciAd, istifadeciParol
列,則需要將其讀取為
string istifadeciAd = rdr.GetString(0);
string istifadeciParol = rdr.GetString(1);
而且您不需要將結果強制轉換為string
,因為它返回的是string
我認為您也需要更改參數並添加代碼,
cmd.Parameters.AddWithValue("@name", TextBox1.Text);
cmd.Parameters.AddWithValue("@password", TextBox2.Text);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.