2017-12-07 9 views
1

SQL Serverデータベースからパスワードと電子メールを取得するログインページを作成しようとしています。私はパスワードと電子メールを比較したいと思います。テキストボックスからデータベースのデータにデータを比較しようとすると、エラーメッセージが表示される

System.Data.SqlClient.SqlException:

private void buttoninloggen_Click(object sender, EventArgs e) 
{ 
    using (SqlConnection connection = new SqlConnection(connectionstring)) 
    { 
     connection.Open(); 

     string emailinlog = textBoxEmailLogin.Text; 
     string passwordinlog = textBoxPasswordLogin.Text; 
     string vergelijken = "select * from Account where Email = @email and Password = @password"; 

     SqlDataAdapter adapter = new SqlDataAdapter(vergelijken, connection); 

     MessageBox.Show("tot hier is t goed"); 

     using (SqlCommand ophalen = new SqlCommand(vergelijken, connection)) 
     { 
      ophalen.Parameters.AddWithValue("@email", emailinlog); 
      ophalen.Parameters.AddWithValue("@password", passwordinlog); 

      DataTable tafel = new DataTable(); 
      adapter.Fill(tafel); 

      if (tafel.Rows.Count > 0) 
      { 
       MessageBox.Show("ingelogd"); 
      } 
     } 
    } 
} 

は、私は、このエラーメッセージが表示されます 'スカラ変数 "@email" を宣言する必要があります。'

私は間違っていますか?

答えて

-1

が私のプログラムから作業コードにあなたの構文を変更することを検討:

ophalen.Parameters.Add(new SqlParameter("@email", emailinlog)); 
+0

はまだ同じエラーを得た...私はそれが何か他のものだと思います.... – MFox

1

あなたのコードが間違っています。クエリと接続を使用してSqlDataAdapterを定義しますが、それを使用してDataTableを入力しようとする前に、何も実行しません。 @emailまたは@passwordの値があなたが決して伝えないので、それが何であるかは分かりません。

あなたのコードは次のようになります。

private void buttoninloggen_Click(object sender, EventArgs e) 
{ 
    using (SqlConnection connection = new SqlConnection(connectionstring)) 
    { 
     connection.Open(); 
     string emailinlog = textBoxEmailLogin.Text; 
     string passwordinlog = textBoxPasswordLogin.Text; 
     string vergelijken = "select * from Account where Email = @email and Password = @password"; 

     // Moved the 'SqlDataAdapter' further down 
     // SqlDataAdapter adapter = new SqlDataAdapter(vergelijken, connection); 

     MessageBox.Show("tot hier is t goed"); 
     using (SqlCommand ophalen = new SqlCommand(vergelijken, connection)) 
     { 
      ophalen.Parameters.AddWithValue("@email", emailinlog); 
      ophalen.Parameters.AddWithValue("@password", passwordinlog); 
      DataTable tafel = new DataTable(); 

      // SqlDataAdapter is now here 
      // As it has been passed the SqlCommand it understands the parameters 
      // Wrapped in using statement for disposal 
      using (SqlDataAdapter adapter = new SqlDataAdapter(ophalen)) 
      { 
       adapter.Fill(tafel); 
       if (tafel.Rows.Count > 0) 
       { 
        MessageBox.Show("ingelogd"); 
       } 
      } 
     } 
    } 
} 
関連する問題