c 23 execute transact sql

Solutions on MaxInterview for c 23 execute transact sql by the best coders in the world

showing results for - "c 23 execute transact sql"
Lyam
06 Mar 2020
1static public int AddProductCategory(string newName, string connString)
2{
3    Int32 newProdID = 0;
4    string sql =
5        "INSERT INTO Production.ProductCategory (Name) VALUES (@Name); "
6        + "SELECT CAST(scope_identity() AS int)";
7    using (SqlConnection conn = new SqlConnection(connString))
8    {
9        SqlCommand cmd = new SqlCommand(sql, conn);
10        cmd.Parameters.Add("@Name", SqlDbType.VarChar);
11        cmd.Parameters["@Name"].Value = newName;
12        try
13        {
14            conn.Open();
15            newProdID = (Int32)cmd.ExecuteScalar();
16        }
17        catch (Exception ex)
18        {
19            Console.WriteLine(ex.Message);
20        }
21    }
22    return (int)newProdID;
23}