繁体   English   中英

使用2 DataGridView C#WindowsForm将数据插入表(从选择)

[英]Insert data into table(from select) using 2 DataGridView c# windowsForm

请帮助我了解我要去哪里。 好了,走吧! 2 DataGridViews,首先,我将服务存储在第二顺序列表中。 当我按下“保存”按钮时,将发生以下代码:

public void insert_sales_list()
    {
        conn.Open();
        foreach (DataGridViewRow row in dgvService.SelectedRows)
        {               
            SQLiteCommand cmd = new SQLiteCommand("insert into sales_list (sales_created_date, sales_created_name, emp_name, cust_phone, cust_name, planned_date, planned_time, service_name, discount, price, order_id) values (@ocd, @ocn, @emp, @c_phone, @c_name, @p_date, @p_time, @sn, @disc, @price, @o_id)", conn);             
            cmd.Parameters.AddWithValue("@ocd", DateTime.Now);
            cmd.Parameters.AddWithValue("@ocn", lblLoginUser.Text);
            cmd.Parameters.AddWithValue("@emp", dgvOrderList.CurrentRow.Cells[1].Value.ToString());
            cmd.Parameters.AddWithValue("@c_phone", dgvOrderList.CurrentRow.Cells[2].Value.ToString());
            cmd.Parameters.AddWithValue("@c_name", dgvOrderList.CurrentRow.Cells[3].Value.ToString());
            cmd.Parameters.AddWithValue("@p_date", dgvOrderList.CurrentRow.Cells[5].Value);
            cmd.Parameters.AddWithValue("@p_time", dgvOrderList.CurrentRow.Cells[6].Value.ToString());
            cmd.Parameters.AddWithValue("@sn", row.Cells[0].Value.ToString());
            cmd.Parameters.AddWithValue("@disc", dgvOrderList.CurrentRow.Cells[4].Value.ToString());
            cmd.Parameters.AddWithValue("@price", row.Cells[1].Value.ToString());
            cmd.Parameters.AddWithValue("@o_id", dgvOrderList.CurrentRow.Cells["order id"].Value);
            cmd.ExecuteNonQuery();

            string sql = "update order_list set status = 'Saved' where id = '" + dgvOrderList.CurrentRow.Cells["order id"].Value + "'";
            cmd = new SQLiteCommand(sql, conn);
            cmd.ExecuteNonQuery();
        }
        conn.Close();

通过此代码,您看到我只是将数据从订单列表插入到销售列表中,用户从DataGridView.Service中选择一个或多个服务,他可以从列表中获取任何服务。 此代码效果很好。

下一步。 我有另一个桌子,每个服务都有自己的材料,例如-男士理发材料有肥皂,洗发水和薄纸。 而且我需要在SalesMaterials表中插入这些数据。 而且我认为代码是错误的,请帮助我找到此错误? 码:

public void insert_sales_materials()
    {
        conn.Open();
        foreach (DataGridViewRow row in dgvService.SelectedRows)
        {               
            string Query = "insert into sales_list_materials(order_id, material_id, norma, created_name, creation_date) " +
                "values( select '" + dgvOrderList.CurrentRow.Cells["order id"].Value + "', a.material_id, a.norma, '" + lblLoginUser.Text + "', '" + DateTime.Now + "'  from service_materials a where a.service_id = '" + row.Cells[2].Value + "')";
            SQLiteCommand cmd = new SQLiteCommand(Query, conn);
            cmd.ExecuteNonQuery();
        }
        conn.Close();
    }

错误:

附加信息:SQLite错误

“选择”附近:语法错误

好,我知道了! 当您使用select插入数据时,请不要使用单词值=))对所有人正确的代码:

public void insert_sales_materials()
    {
        conn.Open();
        foreach (DataGridViewRow row in dgvService.SelectedRows)
        {               
            string Query = "insert into sales_list_materials(order_id, material_id, norma, created_name, creation_date) " +
                "select '" + dgvOrderList.CurrentRow.Cells["order id"].Value + "', a.material_id, a.norma, '" + lblLoginUser.Text + "', '" + DateTime.Now + "'  from service_materials a where a.service_id = '" + row.Cells[2].Value + "'";
            SQLiteCommand cmd = new SQLiteCommand(Query, conn);
            cmd.ExecuteNonQuery();
        }
        conn.Close();
    }

暂无
暂无

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

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