Get the inserted ID with Scope_Identity()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
DataSet Ds = new DataSet();
SqlDataAdapter Da = new SqlDataAdapter();
SqlConnection SqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings["name_of_connectionstrign"].ConnectionString);
 
try
{
    SqlConn.Open();
    SqlCommand CreateNewSessionCmd = new SqlCommand(@"
        INSERT INTO [table]
        (
            [column]
        )
        VALUES
        (
            [value]
        )
        SELECT SCOPE_IDENTITY()
    , SqlConn);
 
    int insertedID = int.Parse(CreateNewSessionCmd.ExecuteScalar().ToString());
}
catch (Exception ex)
{
 
}

comments

No comments yet.

Sorry, the comment form is closed at this time.