【问题标题】:array parameter insert function for dbconnect with arraylist使用 arraylist 的 dbconnect 的数组参数插入函数
【发布时间】:2016-12-16 23:53:56
【问题描述】:

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

【问题讨论】:

  • 包含要插入的值的数组。
  • 您还需要将"VALUES" + "(" ; 更改为" VALUES" + "(" ;,以便它们是您的表名和关键字VALUES 之间的空格。
  • 我知道我有语法错误,但我需要按钮单击上的函数参数

标签: c# arrays winforms arraylist sql-insert


【解决方案1】:

如果您决定使用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"}) );
}

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 试试foreach (var value in values[0]),因为我认为您使用的是ArrayList,但只使用了1个数组。对列 foreach (var column in columns[0]) 执行相同操作
    猜你喜欢
    • 1970-01-01
    • 2017-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多