为 ASP.NET 网站创建 DAL
Posted
技术标签:
【中文标题】为 ASP.NET 网站创建 DAL【英文标题】:Creating DAL for ASP.NET website 【发布时间】:2013-02-23 20:58:08 【问题描述】:我正在使用 Microsoft Visual Studio DAL
,我正在使用传统的方法来获取/更新数据,以通过从网站数据库的 ItemDetails
表中检索数据来显示网站所列项目的评论, 用于创建 ItemDetails.aspx
文件。我添加了DropDownList Control
来显示其类别中的所有项目。
从下拉列表中选择类别时,它会显示该类别中的所有项目,并附有一个超链接"Show Details"
以在网格视图中显示详细信息。
我是新手,我不知道为 asp.net 网站创建DAL
。需要简单的指南来为 asp.net 网站创建 DAL。帮助将不胜感激。除了SQLadapter
,还有什么其他方法可以创建 DAL。
【问题讨论】:
我个人使用实体框架代码优先(我将表定义为具有强类型引用的类)。它动态生成 Db,然后我使用存储库模式进行查询,例如...User user = UserRepo.Single(x=>x.Username=="Bob");
抛出 UnitOfWork
模式以保存更改,这是一个非常优雅且相对高效的 ORM。请注意,一个主要缺点是批量更新可能很慢 - 当您一次更新数十万条记录时,更容易退回到 Sql 适配器。
我想将它用于有近 15-17 个页面和 60 个数据库表的网站。
这不是一个真正的问题。我在一个有大约 100 个表格和数百个页面的网站上使用过它。批量更新是一个问题的唯一原因是它们没有批量发送到 SQL - 例如它确实 UPDATE A set B=C WHERE Id = 1
、UPDATE A set B=C WHERE Id = 2
等而不是 UPDATE A set B=C WHERE Id IN (1,2)
但除非您对数千个执行相同的更新同时行,这不是问题
【参考方案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
【讨论】:
【参考方案2】:以下是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;
【讨论】:
以上是关于为 ASP.NET 网站创建 DAL的主要内容,如果未能解决你的问题,请参考以下文章