2013-03-08 18 views
5

Pracuję nad programem Microsoft Visual Studio DAL, w którym wykonuję tradycyjną metodę pobierania/aktualizowania danych w celu wyświetlenia recenzji wymienionych elementów witryny przez pobranie danych z tabeli ItemDetails baza danych serwisu WWW do tworzenia pliku ItemDetails.aspx. Dodałem DropDownList Control, aby wyświetlić wszystkie elementy w obrębie jego kategorii. Po wybraniu kategorii z listy rozwijanej pokazuje wszystkie pozycje w tej kategorii, z hiperłączem dołączonym do niej, aby wyświetlić szczegóły w widoku siatki. Jestem początkującym nie mam pojęcia, aby utworzyć DAL dla strony asp.net. Potrzebujesz łatwych wskazówek do stworzenia DAL dla strony asp.net. Pomoc zostanie doceniona. Jakie są inne sposoby tworzenia DAL zamiast SQLadapter.Tworzenie DAL dla strony ASP.NET

+1

ja osobiście używać Entity Framework code-pierwszy (I definiować moje tabele klas z strongly- wpisane odniesienia). Generuje dynamicznie Db, a następnie używam wzorca repozytorium do zapytania np. 'User user = UserRepo.Single (x => x.Username ==" Bob ");' Wrzuć wzorzec 'UnitOfWork' dla zapisania zmian i jest to naprawdę elegancka i stosunkowo wydajna ORM. Należy zauważyć, że jedną główną wadą jest to, że aktualizacje zbiorcze mogą być wolne - aktualizując setki tysięcy rekordów naraz, łatwiej jest wrócić do adaptera Sql. – Basic

+0

Chcę go używać na stronie ma prawie 15-17 stron i 60 tabel bazy danych. –

+1

To naprawdę nie jest problem. Używałem go na stronie zawierającej ~ 100 tabel i setki stron. Jedynym powodem, dla którego aktualizacje zbiorcze są problematyczne, jest to, że nie są one wysyłane do SQL w partiach - np. Robi to "UPDATE A set B = C WHERE Id = 1", 'UPDATE A set B = C WHERE Id = 2', etc zamiast 'UPDATE A set B = C WHERE Id IN (1,2)' ale jeśli nie wykonujesz tej samej aktualizacji dla tysięcy wierszy jednocześnie, to nie jest to problem – Basic

Odpowiedz

1

Oto przykład DAL, którego użyłem wcześniej do wywoływania SP.

To pozwala na wykonywanie procedur przechowywanych i powrót zbioru danych, DataTables, odpowiedzi sukces itp

naprawdę to zależy od tego jak masz zamiar uzyskać dostęp do danych, będzie pisanie procedur przechowywanych lub będziesz miał pytania w twoim kodzie. Możesz także użyć Entity Framework/LINQ.

using System; 
using System.Collections.Generic; 
using System.Web; 
using System.Data; 
using System.Data.Sql; 
using System.Data.SqlClient; 
using System.Configuration; 

public class _DataInteraction 
{ 

    #region "Stored Procedures" 

    public static DataTable stdReturnDataTableQuery(string procedureName, string db) 
    { 
     DataTable myDataTable; 

     SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[db].ConnectionString); 
     SqlCommand cmd = new SqlCommand(); 
     SqlDataAdapter myDataAdapter = new SqlDataAdapter(); 

     cmd.CommandText = procedureName; 
     cmd.CommandType = CommandType.Text; 
     cmd.Connection = myConnection; 


     //----------------------------------------------------------------------- 
     // make our datatable to return 
     //----------------------------------------------------------------------- 
     myDataTable = new DataTable(); 

     //----------------------------------------------------------------------- 
     // fill the datatable with the stored procedure results 
     //----------------------------------------------------------------------- 
     try 
     { 
      myConnection.Open(); 
      myDataAdapter.SelectCommand = cmd; 
      myDataAdapter.Fill(myDataTable); 
     } 
     catch (Exception ex) 
     { 
      //flag as error happened 
      throw ex; 
     } 
     finally 
     { 
      myConnection.Close(); 
      if ((myDataAdapter != null)) 
       myDataAdapter.Dispose(); 
      if ((cmd != null)) 
       cmd.Dispose(); 
     } 

     return myDataTable; 
    } 


    // Return a datatable from the database 
    public static DataTable stdReturnDataTable(string procedureName, List<SqlParameter> myParameters, string db) 
    { 
     SqlConnection myConnection = default(SqlConnection); 
     SqlCommand myCommand = default(SqlCommand); 
     SqlDataAdapter myDataAdapter = default(SqlDataAdapter); 
     DataTable myDataTable = default(DataTable); 
     string connString = null; 

     // ----------------------------------------------------------------------- 
     // create instance of connection 
     // ----------------------------------------------------------------------- 
     connString = ConfigurationManager.ConnectionStrings[db].ConnectionString; 
     myConnection = new SqlConnection(); 
     myConnection.ConnectionString = connString; 

     //----------------------------------------------------------------------- 
     // create instance of command and dataadapter 
     //----------------------------------------------------------------------- 
     myCommand = new SqlCommand(procedureName, myConnection); 
     myDataAdapter = new SqlDataAdapter(myCommand); 

     //----------------------------------------------------------------------- 
     // say its a stored procedure command 
     //----------------------------------------------------------------------- 
     myCommand.CommandType = CommandType.StoredProcedure; 

     //----------------------------------------------------------------------- 
     // add any parameters? 
     //----------------------------------------------------------------------- 
     if ((myParameters != null)) 
     { 
      foreach (SqlParameter myParm in myParameters) 
      { 
       // add the parameter to the command 
       myCommand.Parameters.Add(myParm); 
      } 
     } 

     //----------------------------------------------------------------------- 
     // make our datatable to return 
     //----------------------------------------------------------------------- 
     myDataTable = new DataTable(); 

     //----------------------------------------------------------------------- 
     // fill the datatable with the stored procedure results 
     //----------------------------------------------------------------------- 
     try 
     { 
      myConnection.Open(); 
      myDataAdapter.Fill(myDataTable); 
     } 
     catch (Exception ex) 
     { 
      //flag as error happened 
      throw ex; 
     } 
     finally 
     { 
      myConnection.Close(); 
      if ((myDataAdapter != null)) 
       myDataAdapter.Dispose(); 
      if ((myCommand != null)) 
       myCommand.Dispose(); 
     } 

     return myDataTable; 
    } 

    // Return a dataset from the database 
    public static DataSet stdReturnDataset(string procedureName, List<SqlParameter> myParameters, string db) 
    { 
     SqlConnection myConnection = default(SqlConnection); 
     SqlCommand myCommand = default(SqlCommand); 
     SqlDataAdapter myDataAdapter = default(SqlDataAdapter); 
     DataSet ds = new DataSet(); 
     string connString = null; 

     //----------------------------------------------------------------------- 
     // create instance of connection 
     //----------------------------------------------------------------------- 
     connString = ConfigurationManager.ConnectionStrings[db].ConnectionString; 
     myConnection = new SqlConnection(); 
     myConnection.ConnectionString = connString; 

     //----------------------------------------------------------------------- 
     // create instance of command and dataadapter 
     //----------------------------------------------------------------------- 
     myCommand = new SqlCommand(procedureName, myConnection); 
     myDataAdapter = new SqlDataAdapter(myCommand); 

     //----------------------------------------------------------------------- 
     // say its a stored procedure command 
     //----------------------------------------------------------------------- 
     myCommand.CommandType = CommandType.StoredProcedure; 

     //----------------------------------------------------------------------- 
     // add any parameters? 
     //----------------------------------------------------------------------- 
     if ((myParameters != null)) 
     { 
      foreach (SqlParameter myParm in myParameters) 
      { 
       // add the parameter to the command 
       myCommand.Parameters.Add(myParm); 
      } 
     } 

     //----------------------------------------------------------------------- 
     // fill the datatable with the stored procedure results 
     //----------------------------------------------------------------------- 
     try 
     { 
      myConnection.Open(); 
      myDataAdapter.Fill(ds); 
     } 
     catch (Exception ex) 
     { 
      //flag as error happened 
      throw ex; 
     } 
     finally 
     { 
      myConnection.Close(); 
      if ((myDataAdapter != null)) 
       myDataAdapter.Dispose(); 
      if ((myCommand != null)) 
       myCommand.Dispose(); 
     } 

     return ds; 
    } 

    // Return success from a query from the database 
    public static bool db_NonQuerySuccessResponse(string strCommandText, List<SqlParameter> myParameters, string db) 
    { 
     SqlConnection SQLConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[db].ConnectionString); 
     SqlCommand SQLCommand = new SqlCommand(); 
     DataSet ds = new DataSet(); 
     string Value = ""; 
     bool success = false; 

     try 
     { 
      SQLCommand.CommandText = strCommandText; 
      SQLCommand.CommandType = CommandType.StoredProcedure; 
      SQLCommand.Parameters.Clear(); 

      if ((myParameters != null)) 
      { 
       foreach (SqlParameter myParm in myParameters) 
       { 
        // add the parameter to the command 
        SQLCommand.Parameters.Add(myParm); 
       } 
      } 

      SQLCommand.Connection = SQLConnection; 
      SQLConnection.Open(); 
      SQLCommand.ExecuteNonQuery(); 
      SQLConnection.Close(); 

      success = true; 

     } 
     catch (Exception ex) 
     { 
      success = false; 
      return success; 
     } 

     return success; 

    } 

    // General non query, no results no success 
    public static bool db_NonQuery(string strCommandText, List<SqlParameter> myParameters, string db) 
    { 


     SqlConnection SQLConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[db].ConnectionString); 
     SqlCommand SQLCommand = new SqlCommand(); 
     DataSet ds = new DataSet(); 

     try 
     { 
      SQLCommand.CommandText = strCommandText; 
      SQLCommand.CommandType = CommandType.StoredProcedure; 
      SQLCommand.Parameters.Clear(); 

      if ((myParameters != null)) 
      { 
       foreach (SqlParameter myParm in myParameters) 
       { 
        // add the parameter to the command 
        SQLCommand.Parameters.Add(myParm); 
       } 
      } 

      SQLCommand.Connection = SQLConnection; 
      SQLConnection.Open(); 
      SQLCommand.ExecuteNonQuery(); 
      SQLConnection.Close(); 

     } 
     catch (Exception ex) 
     { 
      return false; 
     } 

     return true; 

    } 

    //// Execute scalar on db 
    //public static string db_Scalar(string strCommandText, ref List<SqlParameter> myParameters, string db) 
    //{ 

    // SqlConnection SQLConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[db].ConnectionString); 
    // SqlCommand SQLCommand = new SqlCommand(); 
    // string Value = ""; 

    // SQLCommand.CommandText = strCommandText; 
    // SQLCommand.CommandType = CommandType.StoredProcedure; 
    // SQLCommand.Parameters.Clear(); 


    // if ((myParameters != null)) 
    // { 
    //  foreach (SqlParameter myParm in myParameters) 
    //  { 
    //   // add the parameter to the command 
    //   SQLCommand.Parameters.Add(myParm); 
    //  } 
    // } 

    // SQLCommand.Connection = SQLConnection; 
    // SQLConnection.Open(); 
    // Value = SQLCommand.ExecuteScalar; 
    // SQLConnection.Close(); 
    // return Value; 
    //} 

    #endregion 
} 
0

Poniżej 1 próbka na odniesienie ............

 public List<T> GetRequests(string strNo) 
    { 
     List<T> objlstMapping = null; 
     Mapping objMapping = null; 
     try 
     { 
      Database objDbInstance = CreateSQLDatabase(DbConnection.MF); 

      using (DbCommand objDbCommand = objDbInstance.GetStoredProcCommand(Constants.SP_QUESTS)) 
      { 
       DALBase.AddDbParam(objDbInstance, objDbCommand, "@No", DbType.AnsiString, ParameterDirection.Input, strFolioNo); 

       objDbCommand.Connection = objDbInstance.CreateConnection(); 
       objDbCommand.Connection.Open(); 
       using (DbDataReader dr = objDbCommand.ExecuteReader(CommandBehavior.CloseConnection)) 
       { 
        objMapping = new List<T>(); 
        if (dr.HasRows) 
        { 
         while (dr.Read()) 
         { 
          objMapping = new BrokerFolioMapping(); 
          objMapping .Brok_Code = SProposedValue(dr, "Code"); 
          objMapping .Active = SProposedValue(dr, "Status"); 
          objMapping .AccStmt_Active = SProposedValue(dr, "PortfolioStatus"); 

          objlstFolioMapping.Add(objMapping); 
         } 
        } 
       } 
      } 
     } 
     catch (Exception ex) 
     { 
         } 
     return objlstFolioMapping; 
    }