Monday, March 15, 2010

sql database code and connection

today after long time did sql database code .



string conStr = "Data Source=server;Initial Catalog=Database;Integrated Security=SSPI;"
string conStr = "Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;"

SqlConnection con = new SqlConnection(conStr);
con.Open();
string cmdText = "storedProc";
SqlCommand cmd = new SqlCommand(cmdText,con);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
SqlParameter param = new SqlParameter("@param", SqlDbType.VarChar);
param.Value = "";
cmd.Parameters.Add(param);
int result = cmd.ExecuteNonQuery();

Guys do you also use the same code or have come with some new pattern oriented code

1 comment:

Balaji said...

1) The above code is not unit testable.
I recently used NHibernate for database related operations.
2) The opening/closing of the connection can live in separate class (unit tested separately)
3) The connection string can go into some config providers
4) The actual logic can take a connection object which can point to either actual or in memory database for unit testing