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
5
A
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;
}
Verwandte Themen
- 1. DAL-Generator für PHP
- 2. C# DAL - Schnittstelle für Business-Objekte
- 3. Was von der DAL nach BLL zurückzukehren
- 4. DAL/BL Design Frage
- 5. So erstellen Sie einen Provider unabhängig DAL (.Net)
- 6. DAL ohne web2py
- 7. Business Objekt DAL Design
- 8. DAL Design Frage
- 9. Objektkontext beim Implementieren eines Repository (DAL) und Mocking DAL für TDD
- 10. ASP.NET MVC - Entfernen von Abhängigkeit von DAL in GUI
- 11. Verwendung von Appsettings in der DAL-Schicht
- 12. Paginierung und DAL
- 13. Ninject, Repository und DAL
- 14. Ist Sub-sonic code-generation nur für DAL?
- 15. Gerätetest mit generiertem DAL-Code
- 16. Data Access Layer (DAL) Entwurf
- 17. Manuell geschriebene Business-Objekte oder DAL-Objekte?
- 18. Wie machst du eine generische Auswahlmethode für deine DAL?
- 19. Codebeispiel für Database DAL; "ADO.NET Datasets" und "LINQ to SQL"
- 20. Sind statische Methoden für eine Linq To SQL DAL geeignet?
- 21. php DAL - getrennte Einheit und Datenbank?
- 22. C# DAL Klasse und Business Schicht Klasse
- 23. Wie DataReader zum Komponententest zu spotten DAL
- 24. Wie teste ich meine DAL korrekt?
- 25. Mustername für die Projektion von DAL-spezifischen Klassen (wie LINQ zu SQL-Klassen) zu POCOs
- 26. Abfrage oder Ausdruck für den Ausschluss bestimmter Werte von DAL Auswahl
- 27. C++ DAL - Return Referenz oder bevölkern In Referenz übergeben
- 28. Neues .NET 3.5-Projekt: Welche DAL-Technologie verwenden?
- 29. ADO.NET Entity Framework 4.1 C# DAL Objekte erstellen und hinzufügen mit Schlüssel
- 30. Verschiedene Benutzeroberflächen teilen sich dieselbe BLL und DAL
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
Ich möchte es für die Website verwenden hat fast 15-17 Seiten und 60 Datenbanktabellen. –
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