简体   繁体   English

MySQL / C#插入问题

[英]MySQL/C# Insert Issue

Not sure what is wrong with this 不确定这有什么问题

//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();
        }
    }

This gives me the error System.FormatException: Index (zero based) must be greater than or equal to zero and less than the size of the argument list. 这给我带来错误System.FormatException:索引(从零开始)必须大于或等于零且小于参数列表的大小。

Seems like the size of some of the data is more than the size of the parameter you have specified. 似乎某些数据的大小大于您指定的参数的大小。 for example 例如

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

is the size of the tbName.Text string less than 45. what about using tbName.Text字符串的大小小于45。使用该怎么办

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

You don't need the ToString() on the Text property it already is a string please avoid it. 您不需要Text属性上的ToString() ,因为它已经是字符串了,请避免使用它。

Also

command.Parameters.Add("maxguests", MySqlDbType.Int32, 11, nudNoOfGuests.Value.ToString())

This is expecting a int parameter and you are passing in a string, please use Convert.ToInt32 to pass in the value. 这是一个int参数,您要传入一个字符串,请使用Convert.ToInt32传入该值。

Normally these errors occur due to difference in the number of parameters declared and passed to the command but seems fine in your case. 通常,这些错误是由于声明和传递给命令的参数数量不同而发生的,但对于您而言似乎还不错。 What type of control is nudNoOfGuests maybe that is causing the format error 什么类型的控件是nudNoOfGuests可能导致格式错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM