It probably shouldn't be static. 1) You should probably not keep creating the Connection and Command -- create one and hold onto it (in the closed state). 2) Where does ConnectionString come from? 3) Here's your code with a few changes. It compiles, but it's untested:
public static bool WriteToDatabase(
string sql,
System.Data.CommandType commandType,
out string errorText,
params System.Tuple<string,object>[] values
)
{
bool success = true;
errorText = null;
using (System.Data.IDbConnection connection = new System.Data.SqlClient.SqlConnection(ConnectionString))
{
try
{
using (System.Data.IDbCommand command = connection.CreateCommand() )
{
command.CommandText = sql ;
command.CommandType = commandType ;
foreach (System.Tuple<string,object> param in values )
{
System.Data.IDbDataParameter p = command.CreateParameter() ;
p.ParameterName = param.Item1 ;
p.Value = param.Item2 ;
command.Parameters.Add(p);
}
connection.Open();
command.ExecuteNonQuery();
}
}
catch ( System.Exception err )
{
success = false ;
errorText = err.ToString() ;
}
finally
{
connection.Close();
}
}
return ( success ) ;
}
WriteToDatabase
(
"blah blah blah"
,
System.Data.CommandType.Text
,
out error
,
new System.Tuple<string,object> ( "@date" , System.DateTime.Now )
) ;