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

发布于 2024-08-26 14:24:15 字数 922 浏览 3 评论 0原文

它向我显示此错误 关键字“选择”附近的语法不正确,用于清除员工 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");
    }

it show me this error Incorrect syntax near the keyword 'Select' to make to clear Employee ID in this case is FK in the table (Attendance detail) and the other thing is i am using Data Grid View from another table(Employee information) to Show the list of the staff in my form. then i want to transfer each selected cell value from Data Grid View to attendance detail table. 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");
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

初与友歌 2024-09-02 14:24:15

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

SELECT EmployeeID FROM EmployeeInformation

The syntax of your SQL query is wrong. A SELECT statement requires you to specify the columns you want to return. For example:

SELECT EmployeeID FROM EmployeeInformation
转瞬即逝 2024-09-02 14:24:15

我同意@Darin,但完整的语法是

Insert into attendancedetail (columns list) select columns list from EmployeeInformation where employeeid = ' selected cells value' Values

关键字不是用在这样的声明中。如果选择返回所有需要的列,则第一列列表是可选的。

有关更多示例,请参阅另一个问题

I agree with @Darin, but the full syntax is

Insert into Attendancedetail (columns list) select columns list from EmployeeInformation where employeeid = ' selected cells value'

The Values keyword is not used in such a statement. The 1st columns list is optional if the Select returns all needed columns.

See another question for more examples

拿命拼未来 2024-09-02 14:24:15

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

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

Consider using parameterized queries. Writing code like this will result in sql injection.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文