2013-03-08 93 views
5

我正在使用Microsoft Visual Studio DAL,其中我正在通過從ItemDetails表中檢索數據來獲取/更新數據以顯示列出的網站項目的評論的傳統方法網站數據庫,用於創建ItemDetails.aspx文件。我添加了一個DropDownList Control來顯示其類別中的所有項目。 在從下拉列表中選擇類別時,它會顯示該類別中的所有項目,並附帶一個超鏈接"Show Details"以在網格視圖中顯示詳細信息。 我是新手我不知道爲asp.net網站創建DAL。需要簡單的指導方針爲asp.net網站創建DAL。幫助將不勝感激。創建DAL的其他方法是什麼,而不是SQLadapter爲ASP.NET網站創建DAL

+1

我個人使用實體框架代碼優先(我定義我的表與strongly-類鍵入引用)。它動態生成Db,然後我使用存儲庫模式來查詢,例如...'User user = UserRepo.Single(x => x.Username ==「Bob」);'拋出'UnitOfWork'模式以保存更改這是一個非常優雅和相對高效的ORM。請注意,一個主要缺點是批量更新可能會很慢 - 當您一次更新數十萬條記錄時,更容易退回到Sql適配器。 – Basic 2013-03-08 10:49:08

+0

我想使用它的網站有近15-17頁和60個數據庫表。 – 2013-03-08 10:54:04

+1

這不是一個真正的問題。我在一個擁有100張桌子和數百頁的網站上使用過它。批量更新是一個問題的唯一原因是它們沒有被批量發送到SQL,例如它更新了集合B = C WHERE Id = 1,更新集合B = C WHERE Id = 2等等。而不是'UPDATE A set B = C WHERE Id IN(1,2)''但是除非你同時對數千行執行同樣的更新,這不是一個問題 – Basic 2013-03-08 11:15:23

回答

1

因此,例如,這裏是我以前用於調用SP的DAL。

它可以讓你執行存儲過程和返回數據集,數據表,

真的取決於你打算如何訪問數據成功響應等,您將編寫存儲過程或您將有疑問在你的代碼中。您也可以選擇使用實體框架/ 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

下面是參考1個樣品............

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