2017-05-28 54 views
0

我在登录表单上收到此错误。我不确定是否修复它,因为我不知道什么是错的。InvalidOperationException:无当前行错误

出现InvalidOperationException:没有当前行

当我启动应用程序,它崩溃。

enter image description here

 private void buttonLogin_Click(object sender, EventArgs e) 
    { 
     SQLiteConnection conn = new SQLiteConnection("data source = zivali_v2.sqlite"); 
     conn.Open(); 
     SQLiteCommand com = new SQLiteCommand(conn); 

     com.CommandText = "SELECT * FROM login;"; 

     SQLiteDataReader reader = com.ExecuteReader(); 

      string username = reader["username"].ToString(); 
      string password = reader["password"].ToString(); 

     bool loginValid = false; 
     while (reader.Read()) 
     { 
      if (username == textBoxUserName.Text && password == textBoxPassword.Text) 
      { 
       loginValid = true; 
       break; 

      } 
     } 

     if (loginValid) 
     { 
      MessageBox.Show("OK"); 
     } 
     else 
     { 
      MessageBox.Show("Wrong username or password"); 
     } 
     conn.Close(); 

    } 
+0

嘿,连接字符串有什么问题? 'SQLiteConnection conn = new SQLiteConnection(“data source = zivali_v2.sqlite”);' –

+0

你是什么意思。你认为我的连接是问题吗? – krneki

+0

可能是这样,但奇怪的是你没有在conn.open()函数上发生错误。你试图连接什么数据库? –

回答

0

读者对象表示一个查询的结果,它的形状像一个表,即它具有列和行。

reader["username"]这样的表达式读取指定列和当前行中的值。但在第一个Read()调用之前,没有当前行。这是错误信息告诉你的。

您不能将reader["username"]调用移出循环;你必须在每个循环迭代调用得到各行的新值:

while (reader.Read()) { 
    username = reader["username"] ... 
} 

(而且这将是一个更好的主意,让数据库使用的比较,并use parameters

相关问题