我在另一个数据中插入具有相同id的不同行中的两个数据时遇到问题;
在我的DAL中:
public DataTable test(string name, string course)
{
string insertsql = "INSERT INTO Table1(schName) OUTPUT INSERTED.addID values (@schName)";
SqlCommand cmd = new SqlCommand(insertsql,conn);
cmd.Parameters.AddWithValue("@schName", name);
conn.Open();
var table1Id = (int)cmd.ExecuteScalar();
string insertsql1 = "INSERT INTO Table2(ScholarshipID,DiplomaCourse) values (@id,@course)";
SqlCommand cmd2 = new SqlCommand(insertsql1, conn);
cmd2.Parameters.AddWithValue("@id", table1Id);
cmd2.Parameters.AddWithValue("@course", course);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
da.SelectCommand = cmd2;
DataTable dt = new DataTable();
da.Fill(dt);
conn.Close();
return dt;
}
在我的代码隐藏(cs)
中protected void Button1_Click(object sender, EventArgs e)
{
// addScholarship[] test = new addScholarship[1];
string course = "";
string name = schName.Text;
scholarshipBLL obj = new scholarshipBLL();
List<addScholarship> addScholarshipList = new List<addScholarship>();
addScholarship scholarship;
if (DIT.Checked )
{
scholarship = new addScholarship(name, course);
addScholarshipList.Add(scholarship);
course = "DIT";
DataTable dt = obj.test(name, course);
}
if (DFI.Checked)
{
scholarship = new addScholarship(name, course);
addScholarshipList.Add(scholarship);
course = "DFI";
DataTable dt = obj.test(name, course);
}
}
所以会有两个复选框。现在,如果用户点击一个复选框,它当前有效。当用户单击两个复选框时,它将给出错误,例如:
System.InvalidOperationException:未关闭连接。连接的当前状态是打开的。
现在我关闭了它。一旦我点击提交,它将插入两次。
表1
id schName
1嗨
2嗨
表2
tableId id course
1 1 dfi
2 2 dit
它应该像
表1
id schName
1嗨
表2
tableId Id课程
1 1 dfi
2 1 dit
答案 0 :(得分:1)
您应该将连接对象包装在using语句中。然后编译器会注意应该做什么。
public DataTable test(string name, string course)
{
using(var conn = new SqlConnection(....))
{
string insertsql = "INSERT INTO Table1(schName) OUTPUT INSERTED.addID values (@schName)";
cmd.Parameters.AddWithValue("@schName", name);
conn.Open();
var table1Id = (int)cmd.ExecuteScalar();
string insertsql1 = "INSERT INTO Table2(ScholarshipID,DiplomaCourse) values (@id,@course)";
cmd2.Parameters.AddWithValue("@id", table1Id);
cmd2.Parameters.AddWithValue("@course", course);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
da.SelectCommand = cmd2;
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
}
答案 1 :(得分:1)
再次,你不是在逻辑思考。很明显,您在test1中的table1和table2中都添加了行。因此,如果您将此方法调用两次,则会两次向两个表中添加行。
如果你只想在table1中添加一行,那么你应该为它创建一个单独的方法并只调用一次。你应该创建另一种方法来在table2中添加行,你应该从if块中调用它。
以下是您在奖学金课程中需要的两种方法。
public int AddSholarship(string scholarshipName)
{
using(var conn = new SqlConnection(....))
{
string insertsql = "INSERT INTO Table1(schName) OUTPUT INSERTED.addID values (@schName)";
SqlCommand cmd = new SqlCommand(insertsql,conn);
cmd.Parameters.AddWithValue("@schName", name);
var table1Id = (int)cmd.ExecuteScalar();
return table1Id;
}
}
public DataTable AddCourse(string scholarshipId, string courseName)
{
using(var conn = new SqlConnection(....))
{
string insertsql1 = "INSERT INTO Table2(ScholarshipID,DiplomaCourse) values (@id,@course)";
SqlCommand cmd2 = new SqlCommand(insertsql1, conn);
cmd2.Parameters.AddWithValue("@id", scholarshipId);
cmd2.Parameters.AddWithValue("@course", courseName);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd2;
DataTable dt = new DataTable();
da.Fill(dt);
conn.Close();
return dt;
}
}
按钮_ Click代码应更改如下。
string course = "";
string scholarshipName = schName.Text;
scholarshipBLL obj = new scholarshipBLL();
List<addScholarship> scholarshipList = new List<addScholarship>();
var scholarshipId = obj.AddSholarship(scholarshipName);
addScholarship scholarship;
if (DIT.Checked )
{
scholarship = new addScholarship(Name,course);
scholarshipList.Add(scholarship);
course = "DIT";
DataTable dt = obj.AddCourse(scholarshipId, course);
}
if (DFI.Checked)
{
scholarship = new addScholarship(Name,course);
scholarshipList.Add(scholarship);
course = "DFI";
DataTable dt = obj.AddCourse(scholarshipId, course);
}