gpt4 book ai didi

c# - 插入错误 :Incorrect syntax near the keyword 'SELECT'

转载 作者:太空宇宙 更新时间:2023-11-03 19:26:50 24 4
gpt4 key购买 nike

我正在尝试从 asp.net 表单后面的代码插入,

我有两个表:表培训训练编号培训_日期..

表人培训ID训练编号姓名,部门

我做错了什么吗?

我需要从表单中插入值,另外,从查找表中选择值,其中训练 ID 等于下拉选择的值。

private void InsertInfo()
{

SqlConnection conn = new SqlConnection(GetConnectionString());
string sql = "INSERT INTO personTraining (name,training_id,training,trainingDate.trainingHour,trainingSession) VALUES (@Val1,SELECT (training_id,training,trainingDate.trainingHour,trainingSession FROM tbl_training WHERE training_id = dpDate.selectedValue))";
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.AddWithValue("@Val1", txtName.Text);
//will add rest of form fields

cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}

最佳答案

你需要使用类似的东西

INSERT INTO personTraining
(name,
training_id,
training,
trainingDate.trainingHour,
trainingSession)
SELECT @Val1,
training_id,
training,
trainingDate.trainingHour,
trainingSession
FROM tbl_training
WHERE training_id = dpDate.selectedValue

你不能有这样的混合 VALUES 子句/SELECT 语句。

关于c# - 插入错误 :Incorrect syntax near the keyword 'SELECT' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8012648/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com