如何从数据网格视图列表中插入值?
它向我显示此错误 关键字“选择”附近的语法不正确,用于清除员工 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的 SQL 查询语法错误。
SELECT
语句要求您指定您想要返回的列。例如:The syntax of your SQL query is wrong. A
SELECT
statement requires you to specify the columns you want to return. For example:我同意@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
考虑使用参数化查询。编写这样的代码会导致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