2013-03-08 18 views
5

Ich arbeite an Microsoft Visual Studio DAL in denen ich die traditionelle Methode zum Abrufen/Aktualisieren der Daten, um die Bewertungen der aufgeführten Elemente der Website durch Abrufen von Daten aus der ItemDetails Tabelle von die Website-Datenbank zum Erstellen der Datei . Ich habe einen DropDownList Control hinzugefügt, um alle Artikel innerhalb seiner Kategorien anzuzeigen. Bei der Auswahl der Kategorie aus der Dropdown-Liste werden alle Elemente in dieser Kategorie mit einem Hyperlink "Show Details" angezeigt, um Details in einer Rasteransicht anzuzeigen. Ich bin Neuling Ich habe keine Idee, DAL für asp.net Website zu erstellen. Brauchen Sie einfache Richtlinien, um DAL für asp.net Website zu erstellen. Hilfe wird geschätzt. Was sind die anderen Möglichkeiten zum Erstellen von DAL anstelle von SQLadapter.Erstellen von DAL für ASP.NET-Website

+1

Ich persönlich benutze die Entity Framework-Code-First (definiere ich meine Tabellen als Klassen mit strongly- typisierte Referenzen). Es erzeugt dynamisch den DB und dann benutze ich das Repository-Muster, um zB ... 'User user = UserRepo.Single (x => x.Username ==" Bob ") abzufragen;' Werfen Sie das 'UnitOfWork'-Muster zum Speichern von Änderungen ein und es ist ein sehr elegantes und relativ effizientes ORM. Beachten Sie, dass der Hauptnachteil darin besteht, dass Bulk-Updates langsam sein können - wenn Sie mehrere hunderttausend Datensätze gleichzeitig aktualisieren, ist es einfacher, einen Sql-Adapter zu verwenden. – Basic

+0

Ich möchte es für die Website verwenden hat fast 15-17 Seiten und 60 Datenbanktabellen. –

+1

Das ist nicht wirklich ein Problem. Ich habe es auf einer Website mit ~ 100 Tabellen und Hunderten von Seiten verwendet. Der einzige Grund, warum Bulk-Updates ein Problem darstellen, ist, dass sie nicht in Batches an SQL gesendet werden - zB wird UPDATE A gesetzt B = C WHERE Id = 1, UPDATE A gesetzt B = C WHERE Id = 2, usw Statt "UPDATE A set B = C WHERE Id IN (1,2)" ist es kein Problem, es sei denn, Sie führen das gleiche Update für Tausende von Zeilen gleichzeitig durch – Basic

Antwort

1

Also hier ist zum Beispiel eine DAL, die ich vorher für den Aufruf von SPs benutzt habe.

Es ermöglicht Sie, Stored Procedures und Rückkehr-Datensatz, Datentabellen, Erfolg Antworten usw.

Wirklich es hängt davon ab, auszuführen, wie Sie beabsichtigen auf die Daten zuzugreifen, werden Sie Stored Procedures werden schriftlich oder werden Sie Fragen haben in deinem Code. Sie haben auch die Möglichkeit, Entity Framework/LINQ zu verwenden.

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

Unten ist 1 Probe für Referenz ............

 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; 
    }