Rank: Advanced Member Groups: Member
Joined: 4/3/2008 Posts: 127 Points: 381 Location: Sheffield, UK
|
connection string in asp.nethi all can anybody guide me how to write connection string in web.config and in which segment it would be? and how to access it. thanks in advance
|
Rank: Advanced Member Groups: Member
Joined: 11/9/2007 Posts: 560 Points: 1,011 Location: GB
|
connection string in asp.netsample connect string Code:<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" > </sectionGroup> </configSections> <appSettings> </appSettings> <connectionStrings> <add name="ConnectionString" providerName="System.Data.SqlClient" connectionString="pooling=true; Connect Timeout=30; packet size=4096;user id=username;data source=serverip;persist security info=True;initial catalog=databasename;password=password"/> </connectionStrings> </configuration>
accessing connection string Code: using System; using System.Data; using System.Data.SqlClient;
namespace Microsoft.AdoNet.DataSetDemo { class NorthwindDataSet { static void Main() { string connectionString = GetConnectionString(); ConnectToData(connectionString);
private static void ConnectToData(string connectionString) { //Create a SqlConnection to the Northwind database. using (SqlConnection connection = new SqlConnection(connectionString)) { //Create a SqlDataAdapter for the Suppliers table. SqlDataAdapter adapter = new SqlDataAdapter();
// A table mapping names the DataTable. adapter.TableMappings.Add("Table", "Suppliers");
// Open the connection. connection.Open(); Console.WriteLine("The SqlConnection is open.");
// Create a SqlCommand to retrieve Suppliers data. SqlCommand command = new SqlCommand( "SELECT SupplierID, CompanyName FROM dbo.Suppliers;", connection); command.CommandType = CommandType.Text;
// Set the SqlDataAdapter's SelectCommand. adapter.SelectCommand = command;
// Fill the DataSet. DataSet dataSet = new DataSet("Suppliers"); adapter.Fill(dataSet);
// Create a second Adapter and Command to get // the Products table, a child table of Suppliers. SqlDataAdapter productsAdapter = new SqlDataAdapter(); productsAdapter.TableMappings.Add("Table", "Products");
SqlCommand productsCommand = new SqlCommand( "SELECT ProductID, SupplierID FROM dbo.Products;", connection); productsAdapter.SelectCommand = productsCommand;
// Fill the DataSet. productsAdapter.Fill(dataSet);
// Close the connection. connection.Close(); Console.WriteLine("The SqlConnection is closed.");
// Create a DataRelation to link the two tables // based on the SupplierID. DataColumn parentColumn = dataSet.Tables["Suppliers"].Columns["SupplierID"]; DataColumn childColumn = dataSet.Tables["Products"].Columns["SupplierID"]; DataRelation relation = new System.Data.DataRelation("SuppliersProducts", parentColumn, childColumn); dataSet.Relations.Add(relation); Console.WriteLine( "The {0 DataRelation has been created.", relation.RelationName);
static private string GetConnectionString() { // To avoid storing the connection string in your code, // you can retrieve it from a configuration file. return "Data Source=(local);Initial Catalog=Northwind;" + "Integrated Security=SSPI";
|
Rank: Newbie Groups: Member
Joined: 2/23/2011 Posts: 6 Points: 18 Location: US
|
connection string in asp.netThis site is by no means a complete reference for each of the existing database, but only an attempt to make our fellow software developers to provide a convenient reference to the database connection string. <%@ Import Namespace="System.Data.OleDb" %> <script runat="server"> sub Page_Load dim dbconn dbconn=New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; data source=" & server.mappath("northwind.mdb")) dbconn.Open() end sub </script> psd to xhtml | psd to html | convert to wordpress
|