WEB开发网
开发学院数据库MSSQL Server 基于SQL Server 的Exception Handling - PART II 阅读

基于SQL Server 的Exception Handling - PART II

 2008-12-06 10:15:28 来源:WEB开发网   
核心提示: · Delete Data: P_CLEAR_DATACREATE Procedure P_CLEAR_DATAASDELETE FROM dbo.T_USERS_IN_ROLESDELETE FROM dbo.T_USERSDELETE FROM dbo.T_ROLESGO

·     Delete Data: P_CLEAR_DATA

CREATE Procedure P_CLEAR_DATA
AS
  
  DELETE FROM dbo.T_USERS_IN_ROLES
  DELETE FROM dbo.T_USERS
  DELETE FROM dbo.T_ROLES
  
GO

4.  Common Function:Utility. ExecuteCommand

private const string connectionStringName = "TestDb";
public static bool ExecuteCommand(string procedureName, Dictionary<string, object> parameters)
    {
      ConnectionStringSettings connectionStringSection = ConfigurationManager.ConnectionStrings[connectionStringName];
      DbProviderFactory dbProviderFactory = DbProviderFactories.GetFactory(connectionStringSection.ProviderName);
      using (DbConnection connection = dbProviderFactory.CreateConnection())
      {
        connection.ConnectionString = connectionStringSection.ConnectionString;
        connection.Open();
        DbCommand command = connection.CreateCommand();
        command.CommandText = procedureName;
        command.CommandType = CommandType.StoredProcedure;
        DbParameter parameter;
        foreach (KeyValuePair<string, object> param in parameters)
        {
          parameter = dbProviderFactory.CreateParameter();
          parameter.ParameterName = param.Key;
          parameter.DbType = DbType.String;
          parameter.Value = param.Value;
          command.Parameters.Add(parameter);
        }
        DbTransaction transation = connection.BeginTransaction();
        command.Transaction = transation;
        try
        {
          command.ExecuteNonQuery();
          transation.Commit();
          return true;
        }
        catch
        {
          transation.Rollback();
          throw;
        }
      }
}

上一页  2 3 4 5 6 7 8 9  下一页

Tags:基于 SQL Server

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接