How to connect to a local SQL Server database?

I am creating a web application / website using ASP.net in visual studio 2010. We have our base website, and I even created a SQL Server database, which is located in the App_Data folder of my web application folder.

I created tables and several procedures, but I don’t know how my web forms or their controller classes (C #) access the tables. Below is my rough setup to access it. I do not know how to set the string equal. The database is located in webapplication1/App_Data/database.mdf .

The file I want to access is webapplication/App_Code/DataConnect.cs . What should be the line. What do I need to do to check it out?

 { SqlConnection _sqlConn = null; string _connectionString = ? _sqlConn2 = new SqlConnection(_connectionString); _sqlConn.Open(); } 
+4
source share
5 answers

You can use the following connection string.

 string _connectionString =@ "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True" 

You can also add the connection string to the web.config connectionString section, and then use it in the code.

 <connectionStrings> <add name="CnStr" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/> </connectionStrings> 

To get connectionString from web.config

 string _connectionString=System.Configuration.ConfigurationManager.ConnectionStrings["CnStr"].ConnectionString; 
+7
source

You can manually write the connection string in your code ...

 string strcon = @"Data Source=SERVERNAME; Initial Catalog=DATABASENAME; Integrated Security=True"; 

OR

Follow the instructions below to connect to your local SQL Server database ...

  • Go to View> Server Browser / Database Explorer
  • Right click on Data Connection> Add Connection ...
  • Select the server name, select the authentication type, select the created database.
  • Check the connection and then click OK.
  • Right-click on the database> Properties and use the connection string ...

Check out the link below to find out more.

+2
source

connection string is exactly the same as

 string _connectionString =@ "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|Database.mdf;Integrated Security=True;User Instance=True" 

if you have a problem with the connection string, refer to http://www.connectionstrings.com/

0
source

You can try using the following methods:

 string _connectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename="+ Server.MapPath("~/App_Data") +@ "\database.mdf;Integrated Security=True;User Instance=True" 

or

 string _connectionString =@ "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\database.mdf;Integrated Security=True;User Instance=True" 
0
source

string _connectionString = @ "Data source =. \ SQLEXPRESS; AttachDbFilename = | DataDirectory | Database.mdf; Integrated Security = True; User instance = True

0
source

Source: https://habr.com/ru/post/1384832/


All Articles