asp.net connection database reading data sample sharing

Source: Internet
Author: User
Tags mssql connectionstrings

  This article mainly introduces the ASP.net connection database reading data samples, we refer to the use of the bar

Webconfig configuration:     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: The 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> &nbs P           MS sql<asp:gridview id= "GridView1" runat= "Server" >         &L t;/asp:gridview>     </div>     </form> </body> </html>     Background code :     Code 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 to have a using using System.Configuration;   public partial class DB:System.Web.UI.Page {    protected void Page_Load (object sender, EventArgs e) & nbsp   {        DataSet dsmssql = GetDataSet ("select * from [Table]", "MSSQL");         this. Gridview1.datasource = Dsmssql;         this. Gridview1.databind ();    }       protected DataSet getdataset (String sqlcommand,string DB)     {  &NBS P     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.