我有一個表單,我有一個列表框來選擇不同日期的多個值。但要求是一個日期可以預訂2個用戶只有一次2個用戶預訂日期,然後我只需要從列表框中刪除它。檢查行數,然後插入值
我創建了3個表格。一個表(table_dates)僅顯示不同行中的日期,第二個表(table_users)存儲用戶信息,第三個表(table_map)將用戶映射到日期。
如何編寫一個邏輯來檢查選定日期是否已被2個用戶註冊?
請檢查下面
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "INSERT INTO table_users(Name,Email) OUTPUT INSERTED.userId Values (@name,@email)";
cmd.Parameters.AddWithValue("@name", name);
cmd.Parameters.AddWithValue("@email", email);
int lastId = (int)cmd.ExecuteScalar();
if (lastVolId > 0)
{
SqlCommand cmd2 = new SqlCommand();
int counter = 0;
string query = "";
foreach (ListItem li in listBox.Items)
{
if (li.Selected)
{
// I need to write some thing here to check if selected date is registered 2 times
query = "INSERT INTO table_map(userId,dateId) VALUES('" + lastId + "','" + li.Value + "')";
cmd2 = new SqlCommand(query, conn);
cmd2.ExecuteNonQuery();
counter++;
}
}
}
else
{
//Error notification
}
}
我編輯了您的標題。請參閱:「[應該在其標題中包含」標籤「](http://meta.stackexchange.com/questions/19190/)」,其中的共識是「不,他們不應該」。 – 2015-01-09 20:28:46
爲什麼你在第一次插入時使用參數,然後在第二次插入時不使用它們?您應該始終使用參數化查詢。 – 2015-01-09 21:38:45