C # ASP. NET configuration file connect to SQL database

Source: Internet
Author: User
Tags connectionstrings

Reference first

Using system.configuration;//configuration file
Using System.Data.SqlClient;

I'm using SQL Server. SA user password also SA, development tools for visual studio2012

Front Code:

1<connectionStrings>2<!--<add Name="defaultconnection"Providername="System.Data.SqlClient"connectionstring="data source= (LocalDb) \v11.0;initial catalog=aspnet-configuration file link database -20150623105832;integrated Security=sspi; attachdbfilename=| datadirectory|\aspnet-configuration file link Database -20150623105832.mdf"/>-->3<add name="myconn"connectionstring="Data Source=.;i Nitial Catalog=northwind; Persist Security info=true; User Id=sa;  Password=sa; Database=test1"/>4</connectionStrings>

Background code:

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.Data;//using MySql.Data.MySqlClient;//MysqlusingMySql.Data.MySqlClient;//SQLusingSystem.Configuration;//configuration fileusingSystem.Data.SqlClient;namespaceconfiguration file link Database { Public Partial classTest:System.Web.UI.Page {protected voidPage_Load (Objectsender, EventArgs e) {  //get the connection string in the configuration file            stringConstr = configurationmanager.connectionstrings["myconn"].            ToString (); using(SqlConnection con=NewSqlConnection (CONSTR)) {                stringSelstr ="select name from user1 where 1=1"; SqlCommand com=NewSqlCommand (Selstr,con); Con.                Open (); SqlDataReader SDR=com.                 ExecuteReader ();  while(SDR). Read ()) {LISTBOX1.ITEMS.ADD (sdr[0].                 ToString ());                    }} response.write (Constr); }    }}

Thanks to clerk's predecessors ' experience: http://www.cnblogs.com/yagzh2000/archive/2013/03/13/2957266.html

C # ASP. NET configuration file connect to SQL database

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.