c# sqlconnection
时间:2011-04-26 来源:鱼不爱水
SqlConnection con = null;
SqlCommand cmd = null;
try
{
con = new SqlConnection(ConfigHelper.ConnectionStrings.Default);
con.Open();
cmd = new SqlCommand("ps_Fwk_InsertDiagnostic", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("Category", (cat != null) ? cat.Replace('\'', '"') : string.Empty));
cmd.Parameters.Add(new SqlParameter("Label", label.Replace('\'', '"')));
cmd.Parameters.Add(new SqlParameter("ClientIP", _context.Request.UserHostAddress));
cmd.Parameters.Add(new SqlParameter("ClientBrowser", _context.Request.Browser.Capabilities[string.Empty]));
if (_context.Request.Headers["GUID"] != null)
{
Guid userGuid = new Guid(_context.Request.Headers["GUID"]);
cmd.Parameters.Add(new SqlParameter("UserGuid", userGuid));
}
else
{
cmd.Parameters.Add(new SqlParameter("UserGuid", null));
}
cmd.ExecuteNonQuery();
}
//catch
//{
// throw;
//}
finally
{
if ((con != null) && (con.State != ConnectionState.Closed))
cmd.Connection.Close();
}
string[] userDetails = new string[2];
SqlConnection con = new SqlConnection(Application.Helpers.ConfigHelper.ConnectionStrings.Default);
SqlCommand cmd = new SqlCommand(
string.Format("SELECT UserName, ClearTextPwd FROM dbo.[User] WHERE Guid = '{0}'", guid)
, con);
cmd.CommandType = CommandType.Text;
try
{
con.Open();
using (SqlDataReader rd = cmd.ExecuteReader())
{
if (rd.Read())
{
userDetails[0] = (string)rd.GetValue(0);
userDetails[1] = (string)rd.GetValue(1);
}
}
return userDetails;
}
catch
{
throw;
}
finally
{
if (con.State != ConnectionState.Closed) con.Close();
}