簡體   English   中英

如何將來自mysql查詢的數據存儲在變量中

[英]how to store data from mysql query in a variable

當我執行查詢時,我希望獲得的結果存儲在變量(a)中,然后我希望將該變量轉換為 int 並乘以另一個文本框(textbox1)的值,並將其乘積存儲在另一個文本框(textbox2)。 這是整個表單的代碼

    private void menustart_Load(object sender, EventArgs e)
    {
        // TODO: This line of code loads data into the 'cmsDataSet.menulist' table. You can move, or remove it, as needed.



        String con = "datasource = localhost; port=3306;username=root;password=brianfdes93";
        MySqlConnection mycon = new MySqlConnection(con);

        //wine
        string qw = "select wine from cms.menulist";
        MySqlDataAdapter da = new MySqlDataAdapter(qw, mycon);
        DataTable dt1 = new DataTable();
        da.Fill(dt1);
        foreach (DataRow row in dt1.Rows)
        {
            string wine = string.Format("{0}", row.ItemArray[0]);
            comboBox1.Items.Add(wine); 
        }

        //nvstarter
        string qsnv = "select st_nonveg from cms.menulist";
        MySqlDataAdapter da1 = new MySqlDataAdapter(qsnv, mycon);
        DataTable dt2 = new DataTable();
        da1.Fill(dt2);
        foreach (DataRow row in dt2.Rows)
        {
            string nvs = string.Format("{0}", row.ItemArray[0]);
            comboBox2.Items.Add(nvs);
        }

        //vstarter
        string qsv = "select st_veg from cms.menulist";
        MySqlDataAdapter da3 = new MySqlDataAdapter(qsv, mycon);
        DataTable dt3 = new DataTable();
        da3.Fill(dt3);
        foreach (DataRow row in dt3.Rows)
        {
            string vs = string.Format("{0}", row.ItemArray[0]);
            comboBox3.Items.Add(vs);
        }

        //rice n v
        string qrnv = "select rice_nonveg from cms.menulist";
        MySqlDataAdapter da4 = new MySqlDataAdapter(qrnv, mycon);
        DataTable dtrnv = new DataTable();
        da4.Fill(dtrnv);
        foreach (DataRow row in dtrnv.Rows)
        {
            string rnv = string.Format("{0}", row.ItemArray[0]);
            comboBox4.Items.Add(rnv);
        }

        // rice v
        string qrv = "select rice_veg from cms.menulist";
        MySqlDataAdapter da4i = new MySqlDataAdapter(qrv, mycon);
        DataTable dtrv = new DataTable();
        da4i.Fill(dtrv);
        foreach (DataRow row in dtrv.Rows)
        {
            string rv = string.Format("{0}", row.ItemArray[0]);
            comboBox4i.Items.Add(rv);
        }



        //nvgravy
        string qgnv = "select g_nonveg from cms.menulist";
        MySqlDataAdapter da5 = new MySqlDataAdapter(qgnv, mycon);
        DataTable dtgnv = new DataTable();
        da5.Fill(dtgnv);
        foreach (DataRow row in dtgnv.Rows)
        {
            string gnv = string.Format("{0}", row.ItemArray[0]);
            comboBox5.Items.Add(gnv);
        }

        //vgravy
        string qgv = "select g_veg from cms.menulist";
        MySqlDataAdapter da6 = new MySqlDataAdapter(qgv, mycon);
        DataTable dtgv = new DataTable();
        da6.Fill(dtgv);
        foreach (DataRow row in dtgv.Rows)
        {
            string gv = string.Format("{0}", row.ItemArray[0]);
            comboBox6.Items.Add(gv);
        }

        //sanv
        string qsanv = "select s_nonveg from cms.menulist";
        MySqlDataAdapter da7 = new MySqlDataAdapter(qsanv, mycon);
        DataTable dtsanv = new DataTable();
        da7.Fill(dtsanv);
        foreach (DataRow row in dtsanv.Rows)
        {
            string sanv = string.Format("{0}", row.ItemArray[0]);
            comboBox7.Items.Add(sanv);
        }

        //sav
        string qsav = "select s_veg from cms.menulist";
        MySqlDataAdapter da8 = new MySqlDataAdapter(qsav, mycon);
        DataTable dtsav = new DataTable();
        da8.Fill(dtsav);
        foreach (DataRow row in dtsav.Rows)
        {
            string sav = string.Format("{0}", row.ItemArray[0]);
            comboBox8.Items.Add(sav);
        }
        //dessert
        string qd = "select dessert from cms.menulist";
        MySqlDataAdapter da9 = new MySqlDataAdapter(qd, mycon);
        DataTable dtd = new DataTable();
        da9.Fill(dtd);
        foreach (DataRow row in dtd.Rows)
        {
            string sav = string.Format("{0}", row.ItemArray[0]);
            comboBox9.Items.Add(sav);
        }



        mycon.Close();

        // to populate iod

        int nxtorder;

        String coni = "datasource = localhost; port=3306;username=root;password=brianfdes93";
        MySqlConnection myconi = new MySqlConnection(coni);
        string query = "select MAX(o_id) from cms.order";
        MySqlCommand o = new MySqlCommand(query, myconi);
        myconi.Open();

        MySqlDataReader reader = o.ExecuteReader();
        while (reader.Read())
        {
            if (reader.IsDBNull(reader.GetOrdinal("max(o_id)")))
            {
                nxtorder = 1;
                tid.Text = Convert.ToString(nxtorder);
            }
            else
            {
                nxtorder = Convert.ToInt32(reader["max(o_id)"]);
                tid.Text = Convert.ToString(nxtorder);
            }



        }





       //to fill guest txtbox




    }

    private void button1_Click(object sender, EventArgs e)
    {
        starting s = new starting();
        s.Show();
        this.Close();
    }





    private void button2_Click(object sender, EventArgs e)
    {

            tabControl1.SelectedTab = tabPage3;
            t1.Text = t11.Text;
            t2.Text = t12.Text;
            t3.Text = t13.Text;
            t4.Text = t14.Text;
            t4i.Text = t14i.Text;
            t5.Text = t15.Text;
            t6.Text = t16.Text;
            t7.Text = t17.Text;
            t8.Text = t18.Text;
            t9.Text = t19.Text;
            textBox1.Text = label49.Text;

    }

    private void comboBox1_SelectionChangeCommitted(object sender, EventArgs e)
    {

        string c1 = comboBox1.SelectedItem.ToString();
        t1.Text = t1.Text +c1+",";
    }

    private void comboBox2_SelectionChangeCommitted_1(object sender, EventArgs e)
    {
        string c2 = comboBox2.SelectedItem.ToString();
        t2.Text = t2.Text + c2 + ",";
    }

    private void comboBox3_SelectionChangeCommitted_1(object sender, EventArgs e)
    {
        string c3 = comboBox3.SelectedItem.ToString();
        t3.Text = t3.Text + c3 + ",";
    }

    private void comboBox4_SelectedIndexChanged(object sender, EventArgs e)
    {
        string c4 = comboBox4.SelectedItem.ToString();
        t4.Text = t4.Text + c4 + ",";
    }
    private void comboBox4i_SelectedIndexChanged(object sender, EventArgs e)
    {
        string c4i = comboBox4.SelectedItem.ToString();
        t4i.Text = t4i.Text + c4i + ",";
    }
    private void comboBox5_SelectedIndexChanged(object sender, EventArgs e)
    {
        string c5 = comboBox5.SelectedItem.ToString();
        t5.Text = t5.Text + c5 + ",";
    }

    private void comboBox6_SelectedIndexChanged(object sender, EventArgs e)
    {
        string c6 = comboBox6.SelectedItem.ToString();
        t6.Text = t6.Text + c6 + ",";
    }

    private void comboBox7_SelectedIndexChanged(object sender, EventArgs e)
    {
        string c7 = comboBox7.SelectedItem.ToString();
        t7.Text = t7.Text + c7 + ",";
    }

    private void comboBox8_SelectedIndexChanged(object sender, EventArgs e)
    {
        string c8 = comboBox8.SelectedItem.ToString();
        t8.Text = t8.Text + c8 + ",";
    }

    private void comboBox9_SelectionChangeCommitted_1(object sender, EventArgs e)
    {
        string c9 = comboBox9.SelectedItem.ToString();
        t9.Text = t9.Text + c9 + ",";
    }

    private void button1_Click_1(object sender, EventArgs e)
    {
        tabControl1.SelectedTab = tabPage3;
        t1.Text = t21.Text;
        t2.Text = t22.Text;
        t3.Text = t23.Text;
        t4.Text = t24.Text;
        t4i.Text = t24i.Text;
        t5.Text = t25.Text;
        t6.Text = t26.Text;
        t7.Text = t27.Text;
        t8.Text = t28.Text;
        t9.Text = t29.Text;
        textBox1.Text = label47.Text;
    }



    private void button3_Click(object sender, EventArgs e)
    {

        if (t1.Text != "" && t2.Text != "" && t3.Text != "" && t4.Text != "" && t5.Text != "" && t6.Text != "" && t7.Text != "" && t8.Text != "" && t9.Text != "")
        {
        try
        {
            String con = "datasource = localhost; port=3306;username=root;password=brianfdes93";
            MySqlConnection mycon = new MySqlConnection(con);
            string query = "insert into cms.menu(o_id,wine,st_nonveg,st_veg,rice_nonveg,rice_veg,g_nonveg,g_veg,s_veg,s_nonveg,dessert) values('" + this.tid.Text + "','" + this.t1.Text + "','" + this.t2.Text + "','" + this.t3.Text + "','" + this.t4.Text + "','" + this.t4i.Text + "','" + this.t5.Text + "','" + this.t6.Text + "','" + this.t7.Text + "','" + this.t8.Text + "','" + this.t9.Text + "')";
            MySqlCommand orderinsert = new MySqlCommand(query, mycon);
            MySqlDataReader reader;
            mycon.Open();
            reader = orderinsert.ExecuteReader();
            while (reader.Read())
            {
            }

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }


        try
        {
            String con1 = "datasource = localhost; port=3306;username=root;password=brianfdes93";
            MySqlConnection mycon1 = new MySqlConnection(con1);
            string query1 = "insert into cms.order(price_per_plate,total_amount) values('" + this.textBox1.Text + "','" + this.textBox2.Text+ "')";
            MySqlCommand orderinsert1 = new MySqlCommand(query1, mycon1);
            MySqlDataReader reader;
            mycon1.Open();
            reader = orderinsert1.ExecuteReader();
            while (reader.Read())
            {
            }
            MessageBox.Show("MENU PREFERENCE SAVED");
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }







        }
        else
        {
            MessageBox.Show("please complete the form before submitting");
        }

        payment p = new payment();
        this.Hide();
        p.Show();
    }

    private void tabPage3_Enter(object sender, EventArgs e)
    {
        String con2 = "datasource = localhost; port=3306;username=root;password=brianfdes93";
        MySqlConnection mycon2 = new MySqlConnection(con2);
        string query2 = "select guest from cms.order where o_id='" + tid.Text + "'";
        MySqlCommand orderinsert = new MySqlCommand(query2, mycon2);
        string a = "1";
        mycon2.Open();
        //  a = orderinsert.ExecuteScalar().ToString();
        MySqlDataReader dr = orderinsert.ExecuteReader();
        while (dr.Read())
        {
            a = dr.GetString(0);

        }
        string s = textBox1.Text;
        int a1 = Convert.ToInt16(a);
        int a2 = Convert.ToInt32(string.IsNullOrEmpty(textBox1.Text));
        textBox2.Text = Convert.ToString(a1 * a2);
        mycon2.Close();
    }   
}

}

我收到以下錯誤

輸入字符串的格式不正確

我收到a2的上述錯誤。

我該如何解決這個問題?? 有替代方法嗎?

Convert.ToInt16()用於將給定值轉換為 short(int16) 而不是 int(int32)。 雖然它們是整數,但短值應該分配給 short(int16) ,而不是 int(int32)。您需要使用Convert.ToInt32()方法將值轉換為整數。

嘗試這個:

int a2 = Convert.ToInt32(textBox1.Text);

如果 textBox1.Text 為空,則會引發此錯誤。 使用 String.IsNullOrEmpty(textBox1.Text) 檢查 textBox1.Text 是否為空或空,然后再將其轉換為 int16 或 int 32。如果為空,則問題出在文本框本身。

代替 :

int a2 = Convert.ToInt32(string.IsNullOrEmpty(textBox1.Text));

和 :

try
{
    int a2 = Int.Parse(textBox1.Text);
}
catch (Exception ex)
{
    Console.WriteLine("Failed to parse to int! " + ex.Message);
}

string.IsNullOrEmpty 總是返回一個布爾值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM