使用textbox.text匹配数据库

时间:2015-03-30 10:40:06

标签: c# sql

我想要的是匹配文本框和数据库之间的电子邮件和代码。如果匹配,则状态字段将被标记为"是" < ----默认为空

以下示例无论如何都不起作用..如何与数据库值进行比较......

          OleDbConnection connection = new OleDbConnection();
        connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\sim\Desktop\Web.accdb";
        connection.Open();


         OleDbCommand command = new OleDbCommand();
        command.Connection = connection;

        string query = "select * from [Registration]  WHERE [Authentication] = @authentication and [Email] = @email";
        command.CommandText = query;

        command.Parameters.AddWithValue("@authentication", TextBox2.Text);
        command.Parameters.AddWithValue("@email", TextBox1.Text);
         OleDbDataReader reader = command.ExecuteReader();

         if ((reader.Read() == true))
         {
             string query1 = "UPDATE [Registration] SET [State] = 'YES'";
             command.CommandText = query1;

         }

         connection.Close();

1 个答案:

答案 0 :(得分:4)

您可以使用单个查询执行此操作,如下所示:

  using(OleDbConnection connection = new OleDbConnection())
  {
    connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\sim\Desktop\Web.accdb";
    connection.Open();


     OleDbCommand command = new OleDbCommand();
    command.Connection = connection;

    string query = "UPDATE [Registration] SET [State] = 'YES' WHERE [Authentication] = @authentication and [Email] = @email;";

    command.Parameters.AddWithValue("@authentication", TextBox2.Text);
    command.Parameters.AddWithValue("@email", TextBox1.Text);
    command.ExecuteNonQuery();
    connection.close();
  }