簡體   English   中英

從文本框中檢查數據庫數據

[英]Checking database data from textbox

我有一個文本框,它將接受用戶的輸入並搜索插入的數據是否在 SQL 數據庫表中可用。 如果數據在表中,那么它將更新同一行的兩列(time_out 和 day_out)。
否則它會顯示錯誤消息。 下面的這段代碼不起作用。 請幫忙。

try
{

    SqlConnection con3 = new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=db-ub;Integrated Security=True");
    con3.Open();
    SqlCommand cmd2 = new SqlCommand(@"SELECT Count(*) FROM Visitors WHERE Id=@id",con3);
    cmd2.Parameters.AddWithValue("@id", textBox_VIex.Text);

    SqlCommand cmd3 = new SqlCommand("UPDATE Visitors SET Day_Out=@dO,Time_Out=@tO WHERE Id=@id", con3);
    cmd3.Parameters.AddWithValue("@id", 1);
    cmd3.Parameters.AddWithValue("@dO", DateTime.Now);
    cmd3.Parameters.AddWithValue("@tO", DateTime.Now);

    int o = cmd3.ExecuteNonQuery();
    MessageBox.Show("Good Bye!");
    this.Close();
    FormCheck f2 = new FormCheck();
    f2.Show();
}
catch
{
    MessageBox.Show("Error!");
    textBox_VIex.Clear();
}

請參考我對您代碼的更改,

      int o = cmd3.ExecuteNonQuery();

返回受查詢影響的行數。 如果它為零,則表示 id 不在數據庫中。

  try
   {
     SqlConnection con3 = new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=db-ub;Integrated Security=True");
     con3.Open();
     SqlCommand cmd2 = new SqlCommand(@"SELECT Count(*) FROM Visitors WHERE Id=@id",con3);
    cmd2.Parameters.AddWithValue("@id", textBox_VIex.Text);

    SqlCommand cmd3 = new SqlCommand("UPDATE Visitors SET Day_Out=@dO,Time_Out=@tO WHERE Id=@id", con3);
    cmd3.Parameters.AddWithValue("@id", int.Parse(textBox_VIex.Text));
    cmd3.Parameters.AddWithValue("@dO", DateTime.Now);
    cmd3.Parameters.AddWithValue("@tO", DateTime.Now);

    int o = cmd3.ExecuteNonQuery();
    if(o > 0)
       MessageBox.Show("Good Bye!");
    else
      MessageBox.Show("Error!");
      this.Close();
      FormCheck f2 = new FormCheck();
      f2.Show();
   }
   catch
   {
    MessageBox.Show("Error!");
    textBox_VIex.Clear();
   }

查看代碼中的注釋。 首先,這是處理連接等的好方法

int? result = null; // note nullable int
try
{
    Using (SqlConnection con = New SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=db-ub;Integrated Security=True"))
    {
        con.Open();
        // You don't really care to check up front if the record with id=xx exists because
        // if it doesn't, you get 0 records updated
        // unless you do something with the result of that query
        Using (SqlCommandcmd As New SqlCommand("UPDATE Visitors SET Day_Out=@dO,Time_Out=@tO WHERE Id=@id", con))
        {
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.AddWithValue("@id", 1); // <<-- Are you always passing "1"?????
            cmd.Parameters.AddWithValue("@dO", DateTime.Now);
            cmd.Parameters.AddWithValue("@tO", DateTime.Now);        

            result = cmd.ExecuteNonQuery();
        }

        con.Close()
    } 
}
catch 
{
    // optionally, log error or show error msg in second msgBox below. Save it to a variable here
}

// Separate query and result processing

// Check the result. If result is still null, your query failed. If not 0 - you updated your records
if (result != null && (int)result > 0)
{  
    MessageBox.Show("Updated record OK");
}
else
{
    if (result == null)
    {  
        MessageBox.Show("The construct failed"); // optionally add error msg here
    }
    else
    {
       MessageBox.Show("The record I was looking for is not in DB"); 
    }
}      
// I don't know what you do with your form logic
this.Close();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM