Asp.net example of connecting to the database to read data

Source: Internet
Author: User

Webconfig Configuration:

Copy codeThe Code is as follows:
<ConnectionStrings>
<Add name = "MSSQL" connectionString = "Data Source = localhost; Initial Catalog = test; User ID = sa; password = sa;" providerName = "System. data. sqlClient "/>
</ConnectionStrings>

Foreground aspx:

Copy codeThe Code is as follows:
<% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "DB. aspx. cs" Inherits = "DB" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> DB </title>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>
MS web SQL <asp: GridView ID = "GridView1" runat = "server">
</Asp: GridView>
</Div>
</Form>
</Body>
</Html>

Background code:

Copy codeThe Code is as follows:
Using System;
Using System. Collections. Generic;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Data;
Using System. Data. Common; // remember using
Using System. Configuration;

Public partial class DB: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
DataSet dsMSSQL = GetDataSet ("select * from [Table]", "MSSQL ");
This. GridView1.DataSource = dsMSSQL;
This. GridView1.DataBind ();
}

Protected DataSet GetDataSet (string SqlCommand, string DB)
{
DbProviderFactory dbProviderFactory = DbProviderFactories. GetFactory (ConfigurationManager. ConnectionStrings [DB]. ProviderName );
DbConnection dbConnection = dbProviderFactory. CreateConnection ();
DbConnection. ConnectionString = ConfigurationManager. ConnectionStrings [DB]. ConnectionString;

DataSet ds = new DataSet ();

DbCommand dbCommand = dbProviderFactory. CreateCommand ();
DbCommand. Connection = dbConnection;
DbDataAdapter dbDataAdapter = dbProviderFactory. CreateDataAdapter ();
DbCommand. CommandText = SqlCommand;
DbDataAdapter. SelectCommand = dbCommand;
DbDataAdapter. Fill (ds );

Return ds;
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.