我试图在asp.net表单上插入代码,
我有两张桌子: 表培训 training_id training_Date 。
表人培训 ID training_id 名称, 部门
我做错了吗?
我需要从表单中插入值,另外,从查找表中选择值,其中training 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();
}
答案 0 :(得分:3)
您需要使用类似
的内容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
声明。
答案 1 :(得分:3)
试试这个:
INSERT INTO personTraining (
name,
training_id,
training,
trainingDate.trainingHour,
trainingSession)
SELECT
@Val1,
training_id,
training,
trainingDate.trainingHour,
trainingSession
FROM tbl_training tt
WHERE training_id = dpDate.selectedValue