using(SqlConnection oConn = new SqlConnection(m_ConnectionString)) { using (SqlCommand oComm = new SqlCommand()) { Console.WriteLine("Working with T-SQL Stored Procedures"); SqlParameter p1 = new SqlParameter("@FirstName", SqlDbType.VarChar); p1.Value = "David"; SqlParameter p2 = new SqlParameter("@LastName", SqlDbType.VarChar); p2.Value = "POULIN";
oComm.CommandText = "dbo.AddUsers"; oComm.Parameters.AddRange(new SqlParameter[]{p1, p2}); oComm.Connection = oConn; oComm.CommandType = CommandType.StoredProcedure; oConn.Open();
int count = 0; count = oComm.ExecuteNonQuery();
Console.WriteLine("{0} rows inserted.", count);
using(SqlCommand oComm2 = new SqlCommand()) { oComm2.CommandText = "dbo.GetUsers"; oComm2.Connection = oConn; using(SqlDataReader reader = oComm2.ExecuteReader()) { while (reader.Read()) { Console.WriteLine("User : {0} {1}", reader["FirstName"], reader["LastName"]); } } } } }
using (SqlConnection oConn = new SqlConnection(m_ConnectionString)) {
using (SqlCommand oComm = new SqlCommand()) { Console.WriteLine("Working with CLR Stored Procedures"); SqlParameter p1 = new SqlParameter("@FirstName", SqlDbType.VarChar); p1.Value = "Toto"; SqlParameter p2 = new SqlParameter("@LastName", SqlDbType.VarChar); p2.Value = "TATA";
oComm.CommandText = "dbo.AddUsersCLR"; oComm.Parameters.AddRange(new SqlParameter[] { p1, p2 }); oComm.CommandType = CommandType.StoredProcedure; oComm.Connection = oConn; oConn.Open();
int count = 0; count = oComm.ExecuteNonQuery();
Console.WriteLine("{0} rows inserted.", count); using (SqlCommand oComm2 = new SqlCommand()) { oComm2.CommandText = "dbo.GetUsersCLR"; oComm2.CommandType = CommandType.StoredProcedure; oComm2.Connection = oConn; using (SqlDataReader reader = oComm2.ExecuteReader()) { while (reader.Read()) { Console.WriteLine("User : {0} {1}", reader["FirstName"], reader["LastName"]); } } } } } |