Advertisement
joespi

Untitled

Nov 11th, 2020
1,150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.73 KB | None | 0 0
  1. public bool AddNewCustomer(string Name, ref int NewIdentifier)
  2. {
  3.     using (SqlConnection cn = new SqlConnection { ConnectionString = Properties.Settings.Default.ConnectionString })
  4.     {
  5.         using (SqlCommand cmd = new SqlCommand { Connection = cn })
  6.         {
  7.             cmd.CommandText = "INSERT INTO [Customer] (CompanyName) VALUES (@CompanyName); SELECT CAST(scope_identity() AS int);";
  8.             cmd.Parameters.AddWithValue("@CompanyName", Name);
  9.             cn.Open();
  10.             try
  11.             {
  12.                 NewIdentifier = Convert.ToInt32(cmd.ExecuteScalar());
  13.                 return true;
  14.             }
  15.             catch (Exception)
  16.             {
  17.                 return false;
  18.             }
  19.         }
  20.     }
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement