想插入记录返回自动生成的编号,语句无效
目的:新建一个试卷,插入试卷名称,试卷ID为自动生成,返回试卷ID,代码如下,问题是没有插入记录,也没有报错,请大家看看:
string SavePaper()
{
string sQUES_ID = "";
connection.Open();
sSql = "insert into t_Paper where values('" + txtPaperName.Text.Trim() + "');"
+ "select @@identity as paper_id";
SqlCommand cmd = new SqlCommand(sSql, connection);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
sQUES_ID = dr["paper_id"].ToString();
connection.Close();
return sQUES_ID;
}
------解决方案--------------------