c# - MySQL/C# 插入问题

标签 c# mysql

不知道这有什么问题

//Setup MySQL
    private MySqlConnection datconnection = new MySqlConnection();
    //private MySqlDataAdapter datdata = new MySqlDataAdapter();
    DataGrid datgridInfo = new DataGrid();
    int connect;
    private Form_NewEnquiry frm_parent;

    public Form_AddVenue(Form_NewEnquiry frm1)
    {
        InitializeComponent();
        frm_parent = frm1;
    }

    private void btnAdd_Click(object sender, EventArgs e)
    {
        connect = 0;
        try
        {
                datconnection.ConnectionString =
                "server=127.0.0.1;"
                + "database=as;"
                + "uid=a;"
                + "password=a;";
                connect = 1;
        }
        catch (Exception)
        {
            MessageBox.Show("Connection Unavailable");
        }


        //INSERT SQL Code to Insert Venue
        if (connect == 1)
        {
            try
            {
                datconnection.Open();
                MySqlCommand command = new MySqlCommand("INSERT INTO venues (venue_name,venue_address1,venue_address2,venue_town,venue_county,venue_postcode,venue_telephone,venue_fax,venue_email,venue_web,venue_maxguests) VALUES (?,?,?,?,?,?,?,?,?,?,?)", datconnection);
                command.Parameters.Add("name", MySqlDbType.VarChar, 45, tbName.Text.ToString());
                command.Parameters.Add("address1", MySqlDbType.VarChar, 45, tbAddress1.Text.ToString());
                command.Parameters.Add("address2", MySqlDbType.VarChar, 45, tbAddress2.Text.ToString());
                command.Parameters.Add("town", MySqlDbType.VarChar, 45, tbTown.Text.ToString());
                command.Parameters.Add("county", MySqlDbType.VarChar, 45, tbCounty.Text.ToString());
                command.Parameters.Add("postcode", MySqlDbType.VarChar, 10, tbPostcode.Text.ToString());
                command.Parameters.Add("telephone", MySqlDbType.VarChar, 15, tbTelephone.Text.ToString());
                command.Parameters.Add("fax", MySqlDbType.VarChar, 15, tbFax.Text.ToString());
                command.Parameters.Add("email", MySqlDbType.VarChar, 255, tbEmail.Text.ToString());
                command.Parameters.Add("web", MySqlDbType.VarChar, 255, tbWeb.Text.ToString());
                command.Parameters.Add("maxguests", MySqlDbType.Int32, 11, nudNoOfGuests.Value.ToString());
                command.ExecuteNonQuery();
                //datdata.InsertCommand = command;
            }
            catch (Exception eea)
            {
                MessageBox.Show("Error storing data");
                MessageBox.Show(eea.ToString());
                connect = 0;
            }
            finally
            {
                datconnection.Close();
            }
        }
        if (connect == 1)
        {
            MessageBox.Show("Data Saved");

            //Print

            //Close Window
            frm_parent.reloadVenue();
            this.Close();
        }
    }

这给了我错误 System.FormatException:索引(从零开始)必须大于或等于零且小于参数列表的大小。

最佳答案

似乎某些数据的大小超过了您指定的参数的大小。例如

command.Parameters.Add("name", MySqlDbType.VarChar, 45, tbName.Text);

tbName.Text 字符串的大小小于 45。使用

怎么样?
command.Parameters.Add("name", MySqlDbType.VarChar, tbName.Text.Length, tbName.Text);

关于c# - MySQL/C# 插入问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5777450/

相关文章:

c# - 使 EF 使用 App_Data 文件夹中的本地数据库

mysql - phpMyAdmin 无法连接到 MySQL 服务器

c# - 如何从字符串中删除表情符号字符?

c# - 使用 PostMessage 发送正确的 ALT+C

c# - 具有相同 ViewModel 的多个实例的 MVVM-Light Messenger

c# - 字符串的奇怪结果

php - 将 PDO::ATTR_EMULATE_PREPARES 设置为 false 不起作用

mysql - 是否有可能在将恶意 SQL 存储在数据库表中的情况下执行 SQL 注入(inject)攻击?

php - CodeIgniter 3 - 非聚合列分组依据

c# - 内存中的大型数据集 - .NET Framework 和 C#