string SqlStr="insert into tablename values('','','','')"
SqlConnection sqlcn = new SqlConnection(......);
SqlCommand cmd = new SqlCommand(SqlStr,sqlcn);
try
{
sqlcn.Open();
cmd.NoneQueryExcute();
}
catch(SqlException ex)
{
MessageBox.Show(ex.Message);
}
finally
{
sqlcn.Close();
}
SqlConnection sqlcn = new SqlConnection(......);
SqlCommand cmd = new SqlCommand(SqlStr,sqlcn);
try
{
sqlcn.Open();
cmd.NoneQueryExcute();
}
catch(SqlException ex)
{
MessageBox.Show(ex.Message);
}
finally
{
sqlcn.Close();
}
mycommand.EcecuteNonQuery();
.....
mycommand.ExecuteNonQuery();
.....
cmd.EcecuteNonQuery();
mycomsql.ExecuteNonQuery();
.....
......
private static void LogMessage(string message , string logName)
{ SqlConnection connection = null;
SqlCommand command = null;
SqlTransaction myTrans;
try
{
connection = new SqlConnection("……");
string commandString = "INSERT INTO " + logName + " (status, creatTime) VALUES ('" + message + "',getdate())";
command = new SqlCommand(commandString , connection);
connection.Open(); myTrans = connection.BeginTransaction();
command.Transaction = myTrans;
int numrows = command.ExecuteNonQuery();
myTrans.Commit();
}
catch( Exception ex )
{
MessageBox.Show(ex.Message);
}
finally
{
command.Dispose();
connection.Close();
}
}