Turvenn.no
Webutvikler kurs ASP.NET, C#, CSS mm.

WebRessurs.no - Webprogrammering og ressurs innen ASP, ASP.NET, PHP, SQL, HTML, CSS, Javascript, XML, C#, VB, Flash  mm.

Brukernavn: 
Passord:

Registrèr deg Glemt passord?
Logg deg inn




  ASP.net (199)
  ASP 3.0 (111)
  PHP (30)
  HTML (66)
  SQL (89)
  CSS (46)
  XML (7)
  JavaScript (78)
  Diverse kode (13)


  Programmering (22)
  System og drift (15)
  Trafikk og inntekt (11)
  Guider og tips (22)
  Nyttig lesestoff (23)
  Web forum (604)
  Link galleri (565)


  ASP.NET kurs(5)
  CSS kurs (2)
  JQuery kurs (2)


  Øk trafikken (8)
  Facebook App. (4)
  Microsoft CEO (6)


  Server & web
  Internett & epost
  Systemverktøy
  Sikkerhet
  Fildeling
  Lyd & media
  Diverse software


  Domenesalg
  Metagenerator
  Websikre farger
  WebMail


  Bli medlem!
  Siste innlegg
  Gjestebok
  Tips en venn
  Kontakt oss
  Forsiden




Kode Artikler
Linker


Mål internetthastigheten din.


Kodeeksempel på DataAccess

av Øyvind A. Isaksen
 
Dette eksempelet viser hvordan man kan lage sitt eget Data Access i ASP.Net. Her Lager vi en klasse med navn "ConnectionManager" med en metode kalt "GetConnection". Denne metoden sørger for å åpne koblingen til databasen, og returnerer et Connection Objekt klar til bruk. Metoden "GetConnectionToLogDatabase" gjør tilsvarende operasjon, men her mot en annen database. Et tips ved bruk av DataAccess er å benytte "Using" i ASP.Net, det vil sørge for Connection objektet vil bli korrekt lukket / disposed.

namespace ProductIntranet.DataAccess
{
/// <summary>
/// Helper class for managing database connections.
/// </summary>
internal sealed class ConnectionManager
{
/// <summary>
/// Creates a SqlConnection to the Adventureworks database on the local SQL Server 2005 instance.
/// Opens the connection, and returns the connection.
/// </summary>
/// <returns>An open connection to the Adventureworks database</returns>
public static SqlConnection GetConnection()
{
// Get the connection string from the configuration file
string connectionString = ConfigurationManager.ConnectionStrings["Adventureworks"].ConnectionString;

// Create a new connection object
SqlConnection connection = new SqlConnection(connectionString);

// Handle connection events
connection.StateChange += Logger.LogConnectionStateChange;
connection.InfoMessage += Logger.LogConnectionInfoMessage;

// Open the connection, and return it
connection.Open();
return connection;
}

/// <summary>
/// Creates a SqlConnection to the Log database on the local SQL Server 2005 Express instance.
/// Opens the connection, and returns the connection.
/// </summary>
/// <returns>An open connection to the Log database</returns>
public static SqlConnection GetConnectionToLogDatabase()
{
// Get the connection string from the configuration file
string connectionString = ConfigurationManager.ConnectionStrings["LogDatabase"].ConnectionString;

// Create a new connection object
SqlConnection connection = new SqlConnection(connectionString);

// Open the connection, and return it
connection.Open();
return connection;
}
}
}

WebRessurs.no anbefaler:    StackOverflow.com | Experts-Exchange.com | W3schools | ASP.NET | Codeproject | 4Guys
WebRessurs.no er utviklet og drives av SoftMaker
Sett som startside: [ ]. Bokmerk denne siden: [ klikk ]. Sitemap. http://twitter.com/webressurs_no/. Antall brukersesjoner: 14066451.
Copyright WebRessurs.no © 2003 - 2018
Jobbsøk.no - Jobbsøknad, CV, intervju, tips og lenker