繁体   English   中英

C# 多值组合框值到 SQL 服务器数据库

[英]C# multi valued combo box value to SQL Server database

private void filljobid()
{
    try
    {
        string jobid = "";
        int newjobid, oldjobid;

        SqlConnection con = new SqlConnection();
        con.ConnectionString = "Data Source=DESKTOP-CCQ1T25;Initial Catalog=SmartMovers;Integrated Security=True";
        con.Open();

        SqlCommand cmd = new SqlCommand("SELECT MAX(job_id) FROM job", con);

        SqlDataReader reader;
        reader = cmd.ExecuteReader();

        while (reader.Read())
        {
            jobid = reader[0].ToString();
        }

        oldjobid = int.Parse(jobid.ToString());
        newjobid = oldjobid + 1;

        jobidtextbox.Text = newjobid.ToString();
    }
    catch (Exception)
    {
        MessageBox.Show("Error while connecting");
    }
}

private void fillcustomercombox()
{
    SqlConnection con = new SqlConnection();
    con.ConnectionString = "Data Source=DESKTOP-CCQ1T25;Initial Catalog=SmartMovers;Integrated Security=True";
    con.Open();

    DataSet ds = new DataSet();

    SqlCommand cmd = new SqlCommand("SELECT customer_id,(first_name + ' ' + last_name + ' - ' + contact) AS CUSTOMERNAME FROM customer", con);

    SqlDataAdapter da = new SqlDataAdapter();
    da.SelectCommand = cmd;
    da.Fill(ds);

    customeridcombobox.DataSource = ds.Tables[0];
    customeridcombobox.DisplayMember = "CUSTOMERNAME";
    customeridcombobox.ValueMember = "customer_id";

    cmd.ExecuteReader();
    con.Close();

    // CODE FOR DISPLAYING multiple values in another way, but not sure how to retrieve data from this function
    // for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
    // {
    //     customeridcombobox.Items.Add(ds.Tables[0].Rows[i][0] + " - " + ds.Tables[0].Rows[i][1] + " " + ds.Tables[0].Rows[i][2]);
    // }
}

private void filldepotcombox()
{
    SqlConnection con = new SqlConnection();
    con.ConnectionString = "Data Source=DESKTOP-CCQ1T25;Initial Catalog=SmartMovers;Integrated Security=True";
    con.Open();

    DataSet ds = new DataSet();

    SqlCommand cmd = new SqlCommand("SELECT depot_id,(branch_name + ' - ' + region_name + ' - ' + location) AS DEPOTNAME FROM depot", con);

    SqlDataAdapter da = new SqlDataAdapter();
    da.SelectCommand = cmd;
    da.Fill(ds);

    depotidcombobox.DataSource = ds.Tables[0];
    depotidcombobox.DisplayMember = "DEPOTNAME";
    depotidcombobox.ValueMember = "depot_id";

    cmd.ExecuteReader();
    con.Close();
}

private void filljobtypecombox()
{
    SqlConnection con = new SqlConnection();
    con.ConnectionString = "Data Source=DESKTOP-CCQ1T25;Initial Catalog=SmartMovers;Integrated Security=True";
    con.Open();

    DataSet ds = new DataSet();

    SqlCommand cmd = new SqlCommand("SELECT job_type FROM jobtype", con);

    SqlDataAdapter da = new SqlDataAdapter();
    da.SelectCommand = cmd;
    da.Fill(ds);

    jobtypecombobox.DisplayMember = "job_type";
    jobtypecombobox.ValueMember = "job_type";
    jobtypecombobox.DataSource = ds.Tables[0];
    cmd.ExecuteReader();
    con.Close();
}

private void loadingcomboboxesdata_Load(object sender, EventArgs e)
{
    fillcustomercombox();
    filljobid();
    filldepotcombox();
    filljobtypecombox();
}

private void addnewjobbutton_Click(object sender, EventArgs e)
{
    try
    {
        SqlConnection con = new SqlConnection();
        con.ConnectionString = "Data Source=DESKTOP-CCQ1T25;Initial Catalog=SmartMoversDB;Integrated Security=True";
        con.Open();

        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "insert into job (start_location, end_location, depot_id, job_type, customer_id,) values ('" + startlocationtxtbox.Text + "','" + endlocationtxtbox.Text + "','" + depotidcombobox.Text + "','" + jobtypecombobox.Text + "','" + customeridcombobox.Text + "')";
        cmd.ExecuteReader();
        con.Close();

        MessageBox.Show("Added new job");
    }
    catch (Exception)
    {
        MessageBox.Show("ERROR: CANNOT CONNECT TO DATABASE");
    }
}

我想要实现的基本上是获取用户选择的值,该值显示在 valuemember 组合框中,然后将其插入数据库。 现在,当我尝试将数据插入数据库时出现错误。 当我使用单个值执行组合框时,它可以正常工作,但是当我使用多个值时它不起作用。

有人可以关闭这个问题。 我设法解决了我自己的问题。 我不知道这个解决方案是否被认为是好的,但这里是 go。

    private void addnewjobbutton_Click(object sender, EventArgs e)
    {
        using (SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-CCQ1T25;Initial Catalog=SmartMovers;Integrated Security=True"))
        {
            try
            {

                using (var cmd = new SqlCommand("INSERT INTO job(start_location, end_location, depot_id, job_type, customer_id) VALUES ('" + startlocationtxtbox.Text + "','" + endlocationtxtbox.Text + "',@3,@4, @5)"))
                {
                    cmd.Connection = con;
                    //cmd.Parameters.AddWithValue("@1", startlocationtxtbox.SelectedText);
                    //cmd.Parameters.AddWithValue("@2", endlocationtxtbox.SelectedText);
                    cmd.Parameters.AddWithValue("@3", depotidcombobox.SelectedValue);
                    cmd.Parameters.AddWithValue("@4", jobtypecombobox.SelectedValue);
                    cmd.Parameters.AddWithValue("@5",customeridcombobox.SelectedValue);
                    con.Open();
                    if(cmd.ExecuteNonQuery() > 0)
                    {
                        MessageBox.Show("Record inserted");
                    }
                    else
                    {
                        MessageBox.Show("Record failed");
                    }
                }
            }

            catch (Exception)
            {
                MessageBox.Show("ERROR: CANNOT CONNECT TO DATABASE");
            }
        }
    }

暂无
暂无

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

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