dbconnect与arraylist的数组参数插入函数

时间:2016-12-16 23:53:56

标签: c# arrays winforms arraylist sql-insert

我的dbconnect类中有这个主插入函数。我想使用所有形式的这个功能。我称之为功能,但它不起作用。我能做什么 ?我的错在哪里?

这是我的DBConnect类

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", *????* );
    }

2 个答案:

答案 0 :(得分:0)

如果你必须使用// Getting the node HtmlNode node = doc.DocumentNode.SelectSingleNode("//*[@id=\"title - overview - widget\"]/div[2]/div[2]/div/div[2]/div[2]/div/a[4]/meta"); string date = node.Attributes["content"].Value; 这就是它的外观。 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 )

答案 1 :(得分:0)

好吧,我明白了。谢谢你的anseer,但我有一个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[])"