繁体   English   中英

如何从“数据网格视图”列表中插入值?

[英]how i insert values from list of Data Grid View?

它向我显示此错误,以清除关键字'Select'附近的不正确语法,以清除员工ID,在这种情况下,该表格是表中的FK(出勤详细信息),另一件事是我正在使用另一张表中的数据网格视图(员工信息)在我的表格中显示人员列表。 然后我想将每个选定的单元格值从“数据网格视图”传输到出勤明细表。 10q

    private void time_in_Click(object sender, EventArgs e)
    {
        employee_InformationDataGridView.SelectedCells[0].Style.ForeColor = Color.Green;

            time_in.Enabled = false;
            time_out.Enabled = true;

            con = new SqlConnection(GlobalClass.conn);
            con.Open();
            SqlCommand cmd = new SqlCommand("Insert into Attendancedetail  Values(" + "Select from  EmployeeInformation(EmployeeID)" + ",'" + employee_InformationDataGridView.SelectedCells[0] + "','" + DateTime.Now.ToLongDateString() + "','" + null + "','" + null + "','" + DateTime.Now.ToLongTimeString() + "'," + null + ")", con);
            int i = cmd.ExecuteNonQuery();
            MessageBox.Show(i.ToString() + " record inserted");
    }

您的SQL查询的语法错误。 SELECT语句要求您指定要返回的列。 例如:

SELECT EmployeeID FROM EmployeeInformation

我同意@Darin,但完整语法为

插入到Attendancedetail (列列表) 中,从EmployeeInformation中选择列列表 ,其中employeeid ='selected cells value'

在此语句中不使用Values关键字。 如果“选择”返回所有需要的列,则“第一列”列表是可选的。

查看其他问题以获取更多示例

考虑使用参数化查询。 编写这样的代码将导致sql注入。

http://www.codinghorror.com/blog/2005/04/give-me-parameterized-sql-or-give-me-death.html

暂无
暂无

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

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