C# WinForm 新增数据后将identity(1,1)自动增长值返回到前后界面
以下是代码:
try
{
BMBHRSGS.EndCurrentEdit();
HY_MODEL.BasicInfo.dsHRSGS dsChange = (HY_MODEL.BasicInfo.dsHRSGS)MydsHRSGS.GetChanges();
string connStr = ConfigurationManager.ConnectionStrings["hyhr"].ConnectionString;
SqlConnection myConn = new SqlConnection(connStr);
myConn.Open();
string strSql = "INSERT INTO HRSGS(GS003)VALUES(@GS003);SELECT SCOPE_IDENTITY() AS GS001";
SqlCommand addCmd = new SqlCommand();
addCmd.Connection = myConn;
addCmd.CommandText = strSql;
SqlParameter[] myPara = {
new SqlParameter("@GS003",SqlDbType.VarChar,20,"GS003")
};
addCmd.Parameters.AddRange(myPara);
SqlDataAdapter mySda = new SqlDataAdapter();
mySda.TableMappings.Add("Table", "HRSGS");
mySda.InsertCommand = addCmd;
mySda.Update(dsChange);
}
catch(Exception Ex)
{
MessageBox.Show(Ex.Message.ToString());
}
说明:GS001 是自动增长ID值,现在需要新增一笔数据后就把这个自动产生的ID值获取返回到前台,这是SQL:string strSql = "INSERT INTO HRSGS(GS003)VALUES(@GS003);SELECT SCOPE_IDENTITY() AS GS001";
帮忙参谋一下有没有好的方法,谢谢!!