2011-11-14 83 views
0

我創建了一個簡單的asp.net表單,它允許用戶查看培訓的日期列表並註冊該日期,他們手動輸入他們的名稱和employeeid(我不想讓dulpicate僱工IDS),所以我需要弄清楚如何檢查在C#..在插入sql數據庫之前檢查現有記錄

代碼:

public string GetConnectionString() 
    { 
     //sets the connection string from your web config file "ConnString" is the name of your Connection String 
     return System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString; 


    } 

    private void checkContraint() 
    { 

     SqlConnection conn = new SqlConnection(GetConnectionString()); 
     string sql = "Select "; //NEED HELP HERE 

    } 


    private void InsertInfo() 
    { 
     var dateSelected = dpDate.SelectedItem.Value; 
     SqlConnection conn = new SqlConnection(GetConnectionString()); 

     string sql = "INSERT INTO personTraining (name,department,title,employeeid,training_id, training,trainingDate,trainingHour, trainingSession)SELECT @Val1b+','[email protected],@Val2,@Val3,@Val4,training_id,training,trainingDate,trainingHour,trainingSession FROM tbl_training WHERE training_id [email protected]_id "; 


     try 
     { 
      conn.Open(); 
      SqlCommand cmd = new SqlCommand(sql, conn); 


      cmd.Parameters.AddWithValue("@Val1", txtName.Text); 
      cmd.Parameters.AddWithValue("@Val1b", txtLname.Text); 
      cmd.Parameters.AddWithValue("@Val2", txtDept.Text); 
      cmd.Parameters.AddWithValue("@Val3", txtTitle.Text); 
      cmd.Parameters.AddWithValue("@Val4", txtEmployeeID.Text); 

      //Parameter to pass for the select statement 
      cmd.Parameters.AddWithValue("@training_id", dateSelected); 
      cmd.CommandType = CommandType.Text; 
      //cmd.ExecuteNonQuery(); 

      int rowsAffected = cmd.ExecuteNonQuery(); 
      if (rowsAffected == 1) 
      { 

       //Success notification   // Sends user to redirect page 

       Response.Redirect(Button1.CommandArgument.ToString()); 
       ClearForm(); 
      } 
      else 
      { 
       //Error notification  

      } 
     } 
     catch (System.Data.SqlClient.SqlException ex) 
     { 
      string msg = "Insert Error:"; 
      msg += ex.Message; 
      throw new Exception(msg); 

     } 
     finally 
     { 
      conn.Close(); 
     } 
    } 

    protected void Button1_Click(object sender, EventArgs e) 
    { 

     checkContraint(); 
     InsertInfo(); 
+0

您是否需要將其限制爲每個員工ID的一個培訓課程?或者每個日期一個,每個會話一個,等等? –

+0

只需在整個表格中限制一個員工ID(唯一) – user959443

+0

Royi的答案看起來就是這樣。雖然不是檢查約束方法的單獨驗證查詢,但它應該在InsertInfo中打到錯誤通知處理程序。 –

回答

3

這樣,你的查詢將數據插入僅如果不是已經存在

string sql = "INSERT INTO personTraining (name,department,title,employeeid,training_id, training,trainingDate,trainingHour, trainingSession)SELECT @Val1b+','[email protected],@Val2,@Val3,@Val4,training_id,training,trainingDate,trainingHour,trainingSession FROM tbl_training WHERE training_id [email protected]_id and not exists (select 1 from personTraining pp where pp [email protected]) "; 
+0

我從來沒有見過從personTraining pp選擇1,其中pp .employeeid = @ Val4 ....它有什麼作用? – user959443

+0

插入:'xxxxx'到表'vvvvvvvv',但只有'ccccc'並且沒有指定員工ID的記錄 –

+0

雖然是1? – user959443

相關問題