2014-12-06 16 views
0

我成功發佈了格式化字符串以訪問數據庫,但字符串沒有插入到行中,而是它們都排成一行。我不知道如何將數據從富文本字段發佈到ms訪問行

private void viewLog_Click(object sender, EventArgs e)  
    {  
     string status = this.statusDisplay.Text; 
     string time = this.logtimeDispaly.Text; 
     try 
     { 
      for (int rows = 0; rows < status.Length; rows++) 
      { 
       //create a new connection 
       OleDbConnection connection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source = ElevatorStatusLog.mdb"); 
       //open the connection 
       connection.Open(); 
       //create a query 
       OleDbCommand command; 
       command = new OleDbCommand("INSERT INTO StatusLog([DateTime],[Activities]) VALUES(@Parameter1, @Parameter2)", connection); 
       command.Parameters.Add(new OleDbParameter("@Parameter1", time.ToString())); 
       command.Parameters.Add(new OleDbParameter("@Parameter2", status.ToString())); 
       command.ExecuteNonQuery(); 
       connection.Close(); 
      }  

     } 
     catch (Exception err) 
     { 
      MessageBox.Show(err.Message.ToString()); 
     } 

這應該被插入到行中。請幫幫我。謝謝

+0

非常感謝Soner。 – Progzilla 2014-12-06 16:47:22

+0

它會拋出任何異常嗎? – Patrick 2014-12-06 16:50:01

+0

不,它不。它有效但不正確。從富文本中將約4行字符串插入到訪問中的一行中。這就是爲什麼我嘗試使用for循環來糾正這個問題。 – Progzilla 2014-12-06 16:54:16

回答

0

最終我得到了一個高中同學的幫助。我希望它可以幫助未來的學生,因爲我無法在網上找到直接的解決方案。

private void viewLog_Click(object sender, EventArgs e) 
    { 

     try 
     { 
      if(statusDisplay.Lines.Count() > 1){ 
       var total = statusDisplay.Lines.Count()-1; 
       for (int c = 0; c < total; c++) 
       { 

        //create a new connection 
        OleDbConnection connection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source = ElevatorStatusLog.mdb"); 
        //open the connection 
        connection.Open(); 
        //create a query 
        OleDbCommand command = new OleDbCommand("INSERT INTO StatusLog([DateTime],[Activities]) VALUES(@Parameter1, @Parameter2)", connection); 
        command.Parameters.Add(new OleDbParameter("@Parameter1", this.logtimeDispaly.Lines.ElementAt(c).ToString())); 
        command.Parameters.Add(new OleDbParameter("@Parameter2", this.statusDisplay.Lines.ElementAt(c).ToString())); 
        command.ExecuteNonQuery(); 
        connection.Close(); 
       } 
       } 
     } 
     catch (Exception err) 
     { 
      MessageBox.Show(err.Message.ToString()); 
     } 
相關問題