其中sql语句写法为:INSERT INTO table(a,b) VALUES (1,2) SELECT @@IDENTITY AS 'Identity' ,然后用SqlCommand 对象的ExecuteScalar()方法获取即可,下面是写好了的一个函数。
public static int ExecuteSql(string SQLString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand(SQLString, connection))
{
try
{
connection.Open();
int rows = cmd.ExecuteScalar();
return rows;
}
catch (System.Data.SqlClient.SqlException E)
{
connection.Close();
throw new Exception(E.Message);
}
}
}
}