2016-12-16 46 views
0

我在我的dbconnect類中有這個主要的插入函數。我想使用所有的形式這個功能。和我打電話功能,但它不工作。我能做什麼 ?我的錯誤在哪裏?數組參數插入函數dbconnect與arraylist

這是我的數據庫連接類

public bool OpenConnection() 
    { 
     try 
     { 
      connection.Open(); 
      return true; 
     } 
     catch (MySqlException ex) 
     { 
      //When handling errors, you can your application's response based 
      //on the error number. 
      //The two most common error numbers when connecting are as follows: 
      //0: Cannot connect to server. 
      //1045: Invalid user name and/or password. 
      switch (ex.Number) 
      { 
       case 0: 
        MessageBox.Show("Cannot connect to server. Contact administrator"); 
        break; 

       case 1045: 
        MessageBox.Show("Invalid username/password, please try again"); 
        break; 
      } 
      return false; 
     } 
    } 

    //Close connection 
    public bool CloseConnection() 
    { 
     try 
     { 
      connection.Close(); 
      return true; 
     } 
     catch (MySqlException ex) 
     { 
      MessageBox.Show(ex.Message); 
      return false; 
     } 
    } 

    //Insert statement 
    public void Insert(string tablename , ArrayList [] values) 
    { 
     string val = "VALUES" + "(" ; 
      for (int i=0; i<values.Length; i++ ) 
     { 
      if (values.Length > 1) 
       val += values[i] + ","; 
      else val += values[i]; 
     } 
     val += ")"; 
     string query = "INSERT INTO "+ tablename + val ; 

     //open connection 
     if (this.OpenConnection() == true) 
     { 
      //create command and assign the query and connection from the constructor 
      MySqlCommand cmd = new MySqlCommand(query, connection); 

      //Execute command 
      cmd.ExecuteNonQuery(); 

      //close connection 
      this.CloseConnection(); 
     } 
    } 

這裏是調用函​​數按鈕點擊時。我想,所以我沒有找到什麼來這裏「????」

private void button1_Click(object sender, EventArgs e) 
    { 

     DBConnect conn = new DBConnect(); 
     conn.Insert("rezervationinformations", *????*); 
    } 
+0

一個數組你想要插入的值。 – McNets

+0

您還需要更改'「VALUES」+「(」;'''VALUES「+」(「;',以便它們是您的表名和關鍵字VALUES之間的空格 – jaredbaszler

+0

我知道我有一個語法錯誤,但我需要按鈕上的函數參數點擊 –

回答

0

如果你設置爲不得不使用ArrayList這是它應該看起來。 ArrayList是可能導致問題的數組列表。

private void button1_Click(object sender, EventArgs e) 
{ 
    DBConnect conn = new DBConnect(); 
    conn.Insert("rezervationinformations", 
    new ArrayList(new string[] {"value1","value2","value3","value4","value5","value6"})); 
} 

否則只是使用普通的string[]陣列:

public void Insert(string tablename , string[] values)

private void button1_Click(object sender, EventArgs e) 
{ 
    DBConnect conn = new DBConnect(); 
    conn.Insert("rezervationinformations", 
    new string[] {"value1","value2","value3","value4","value5","value6"})); 
} 
0

確定我得到了它。感謝ü爲您anseer但我havea的ExecuteNonQuery錯誤

我的功能:

public void Insert(string tablename ,string[] values, string[] columns) 
    { 
     string val = "VALUES"+"("; 
     foreach (var value in values) 
     { 

      if (values.Length > 1) 
       val += values + ","; 
      if (values == null) 
       val = ""; 
      else val += values; 
     } 
     val += ")"; 
     string col = "("; 
     foreach (var column in columns) 
     { 

      if (columns.Length > 1) 
       col += columns + ","; 
      if (columns == null) 
       col = ""; 
      else col += columns; 
     } 
     col += ")"; 

調用的函數:

conn.Insert("rezervationinformations", new string[] { textBox1.Text, textBox2.Text, textBox3.Text }, new string[] { "FullName", "Phone", "Description" }); 

我的查詢:返回從功能

"INSERT INTO rezervationinformations(System.String[],System.String[]System.String[],System.String[]System.String[],System.String[])VALUES(System.String[],System.String[]System.String[],System.String[]System.String[],System.String[])" 
+0

嘗試'foreach(var value in values [0])'因爲我認爲你使用的是'ArrayList',但只使用1個數組。'foreach (列[0]中的var列)' – jaredbaszler

+0

謝謝你的工作 –