111,094
社区成员




public Class DBBase
{
string connectionString = "***";
//操作sql语句
public bool ExecuteSql(string sql)
{
bool result = false;
SqlConnection connection = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand(sql,connection);
connection.Open();
int i = Convert.ToInt32(cmd.ExecuteNonQuery());
if(i == 1)
result = true;
if(cmd != null)
{
cmd.Cancel();
cmd.Dispose();
}
if(connection != null)
{
connection.Close();
connection.Dispose();
}
return result;
}
}
//调用DBBase
DBBase db = new DBBase();
string sql = "delete from tableUser where id = 1";
bool result = db.ExecuteSql();
public void ExecuteTran(string[] sqls)
{
SqlConnection connection = new SqlConnection(connectionString);
SqlCommand command = connection.CreateCommand();
SqlTransaction transaction = null;
transaction = connection.BeginTransaction(connection);
try
{
conn.Open();
for(int i = 0;i < sqls.Length;i ++)
{
cmdmand.CommandText = sqls[i];
cmdmand.ExecuteNonQuery();
}
transaction.Commit();
}
catch()
{
transaction.Rollback();
}
}