2015-06-23 94 views
0

大家好我是新來的c#和sql,我在c#中創建一個Windows應用程序,將允許用戶不要在每個班次的上午7點到晚上7點之間在數據庫中插入相同的信息,應用程序必須允許用戶在這個時間間隔和同一件事之間在晚上7點和上午之間不插入相同的信息....如果用戶試圖插入相同的數據在此間隔之間會彈出一條消息,說明「這種轉變已經存在信息」。允許用戶每個班次發送一次數據到數據庫(每天一次,每晚一次)

這裏是我到目前爲止的代碼爲SAVE按鈕,它可以插入到數據庫中,我需要有人誰可以幫助在哪?我怎麼可以添加此功能允許沒有用戶在每班數據庫中插入相同的信息早上7點到晚上7點之間,或者如果有另一種方式。有人能幫我嗎?

private void button1_Click(object sender, EventArgs e) 
    { 
     //................INSERT TO DATABASE........................................................... 





     if ((!string.IsNullOrWhiteSpace(comboBox1.Text)) & (!string.IsNullOrWhiteSpace(textBox10.Text)) & (!string.IsNullOrWhiteSpace(textBox9.Text))& (!string.IsNullOrWhiteSpace(textBox6.Text))& (!string.IsNullOrWhiteSpace(textBox8.Text))& (!string.IsNullOrWhiteSpace(textBox13.Text))& (!string.IsNullOrWhiteSpace(textBox7.Text))& (!string.IsNullOrWhiteSpace(textBox3.Text))& (!string.IsNullOrWhiteSpace(textBox2.Text))) 


     { 
      string buffer = textBox10.Text; 
      string buffer1 = textBox9.Text; 
      string buffer2 = textBox6.Text; 
      string buffer3 = textBox7.Text; 
      string buffer4 = textBox8.Text; 

      double val = Double.Parse(buffer, System.Globalization.CultureInfo.InvariantCulture); 
      double val1 = Double.Parse(buffer1, System.Globalization.CultureInfo.InvariantCulture); 
      double val2 = Double.Parse(buffer2, System.Globalization.CultureInfo.InvariantCulture); 

      double val3 = Double.Parse(buffer3, System.Globalization.CultureInfo.InvariantCulture); 

      double val4 = Double.Parse(buffer4, System.Globalization.CultureInfo.InvariantCulture); 


      if (val > 59 && val < 161 && val1 > 99 && val1 < 301 && val2>2&&val2<7 && val3>2 && val3<7 && val4>2 && val2<4) 
      { 


       float em1 = float.Parse(textBox6.Text, System.Globalization.CultureInfo.InvariantCulture); 
       float em2 = float.Parse(textBox7.Text, System.Globalization.CultureInfo.InvariantCulture); 
       float em3 = float.Parse(textBox8.Text, System.Globalization.CultureInfo.InvariantCulture); 
       float ava = float.Parse(textBox13.Text, System.Globalization.CultureInfo.InvariantCulture); 
       int bond = int.Parse(textBox9.Text, System.Globalization.CultureInfo.InvariantCulture); 
       int feed = int.Parse(textBox10.Text, System.Globalization.CultureInfo.InvariantCulture); 



       string Connection = @"Data Source=NZUZO\SQLEXPRESS;Initial Catalog=Checksheet;Integrated Security=True"; 



       string query = "insert into SJB (Machine_Cleanliness,Safety_System,Single_Mode,SJB_Fixture,Feed_Pressure,Bond_Height,Line_NO,Machine_NO,Product,Suspension,Capilary_Lifetime,EM_Trial1,EM_Trial2,EM_Trial3,Avarage,EN,Shift) values(@Machine_C ,@Safety, @SingleMode,@SJB_Fixture,'" + feed + "','" + bond + "','" + this.textBox12.Text + "','" + this.textBox4.Text + "','" + this.textBox1.Text + "','" + this.textBox11.Text + "','" + this.textBox3.Text + "','" + em1 + "','" + em2 + "','" + em3 + "','" + ava + "','" + this.textBox2.Text + "','" + this.label5.Text + "')"; 


       SqlConnection conn = new SqlConnection(Connection); 
       SqlCommand conn1 = new SqlCommand(query, conn); 
       //select getdate()"); 
       conn1.Connection = conn; 
       //String selection = new string(MyChar, 1); 

       //conn1.Parameters.AddWithValue("@Safety", selection); 

       SqlDataReader myReader; 
       // conn1.CommandType = CommandType.StoredProcedure; 
       using (var command = new SqlCommand("Checksheet1", conn) 
       { 
        CommandType = CommandType.StoredProcedure 
       }) 

       // recordCount > 0; 



       // radioButton6 = radioButton6.checked; 

       if (radioButton6.Checked) 
       { 
        // selection = "G"; 
        conn1.Parameters.AddWithValue("@Safety", "G"); 
       } 
       else if (radioButton5.Checked) 


        // selection = "N"; 
        conn1.Parameters.AddWithValue("@Safety", "N"); 

       if (radioButton11.Checked) 
       { 
        conn1.Parameters.AddWithValue("@Machine_C", "G"); 
       } 
       else if (radioButton12.Checked) 

        conn1.Parameters.AddWithValue("@Machine_C", "N"); 

       if (radioButton13.Checked) 
       { 
        conn1.Parameters.AddWithValue("@SingleMode", "G"); 
       } 
       else if (radioButton14.Checked) 

        conn1.Parameters.AddWithValue("@SingleMode", "N"); 

       if (radioButton1.Checked) 
       { 
        conn1.Parameters.AddWithValue("@SJB_Fixture", "G"); 
       } 
       else if (radioButton2.Checked) 
       { 
        conn1.Parameters.AddWithValue("@SJB_Fixture", "N"); 
       } 


       try 
       { 
        conn.Open(); 
        // conn1.ExecuteNonQuery(); 
        myReader = conn1.ExecuteReader(); 



        MessageBox.Show("Machine Checked......"); 

        this.Close(); 




        while (myReader.Read()) 
        { 
       //   conn1.Parameters.AddWithValue("@Line_No",textBox12); 
       //   bool test=false; 
       //int hour = DateTime.Now.Hour; 

       // test = (myReader !=null && myReader.HasRows); 







        } 

        conn.Close(); 
       } 

       catch (Exception es) 
       { 
        MessageBox.Show(es.Message); 
       } 

      } 
      else 
      { 

       MessageBox.Show("Error.... Invalid Value in The check List.", "Check Sheet", MessageBoxButtons.OK, MessageBoxIcon.Error); 

      } 

     } 
     else 
     { 
      MessageBox.Show("Cannot Submit Checklist...Please check for empty space", "Check Sheet", MessageBoxButtons.OK, MessageBoxIcon.Error); 
     } 
    } 
+8

不要用你的TITLE拍攝我們。我們是好人。 –

+1

首先,不要在標題中使用標籤,現在我的選擇。你可以做的是做一個布爾值並且調用這個轉換。然後制定日期時間並設定時間。然後,當用戶插入數據時,布爾值變爲false,然後用戶無法插入數據,直到您設置的時間 –

+1

也請更改組件的名稱,這會使代碼非常不清晰。如果文本框必須有密碼,請將其稱爲passwordTextbox。它更容易這樣 –

回答

0

我想你需要添加另一個調用數據庫來檢查條件是否已經存在row。例如:

select * from table where user_id = 10; 

如果行返回是指用戶已經將數據插入到表中,您還可以對特定日期的用戶是插入檢查...

0

好吧,只是一個簡單的解決方案這個問題,至少我認爲它是一個簡單的解決方案;)。

首先給出文本框名稱。它更容易閱讀,如果有些錯誤,你會更快地理解錯誤的原因。

所以,如果你有一個用於輸入用戶名只是做這樣的一個文本框:

string username = usernameTextbox.Text; 

那麼,爲什麼這麼多的緩衝? 只是做一個List<string> buffer = new List<string> 這裏有一個關於該列表例如:http://www.dotnetperls.com/list

我們awnser的問題,你必須選項的proxyType指出之一。

而且當添加數據時,您可以在數據庫中插入日期時間。然後,當你收到另一個請求時,檢查x時間是否已經過去,以及它是否做了某些事情,否則不要做任何事情並指出給用戶

+0

感謝您的建議傢伙...我將重命名文本字段和緩衝區它只是一個變量聲明,以便我能夠將字符串轉換爲整數並能夠使用邏輯運算符&&來檢查條件 –

+0

好吧,祝你好運 –

0

如果你只想根據時間進行檢查,那麼你可以使用系統日期時間屬性進行檢查。