Using the Web.Config to set up my SQL database connection string?
Here's a great overview on MSDN that covers how to do this.
In your web.config, add a connection string entry:
<connectionStrings>
<add
name="MyConnectionString"
connectionString="Data Source=sergio-desktop\sqlexpress;Initial
Catalog=MyDatabase;User ID=userName;Password=password"
providerName="System.Data.SqlClient"
/>
</connectionStrings>
Let's break down the component parts here:
Data Source is your server. In your case, a named SQL instance on sergio-desktop
.
Initial Catalog is the default database queries should be executed against. For normal uses, this will be the database name.
For the authentication, we have a few options.
User ID and Password means using SQL credentials, not Windows, but still very simple - just go into your Security section of your SQL Server and create a new Login. Give it a username and password, and give it rights to your database. All the basic dialogs are very self-explanatory.
You can also use integrated security, which means your .NET application will try to connect to SQL using the credentials of the worker process. Check here for more info on that.
Finally, in code, you can get to your connection string by using:
ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString
ASP.NET connection string to local SQL Server database, using web.config
When you are working with a localDB you'll have to specify a AttachDbFileName attribute in your connectionString. This attribute should point to your TestSQLdb.mdf file. Initial catalog is the name of your dataBase within your mdf file.
see MSDN for this example
<add name="ConnectionStringName"
providerName="System.Data.SqlClient"
connectionString="Data Source=(LocalDB)\v11.0;AttachDbFileName=|DataDirectory|\DatabaseFileName.mdf;InitialCatalog=DatabaseName;Integrated Security=True;MultipleActiveResultSets=True" />
Setting up connection string in ASP.NET to SQL SERVER
You can also use this, it's simpler. The only thing you need to set is "YourDataBaseName".
<connectionStrings>
<add name="ConnStringDb1" connectionString="Data Source=localhost;Initial Catalog=YourDataBaseName;Integrated Security=True;" providerName="System.Data.SqlClient" />
</connectionStrings>
Where to place the connection string
<?xml version='1.0' encoding='utf-8'?>
<configuration>
<connectionStrings>
<clear />
<add name="Name"
providerName="System.Data.ProviderName"
connectionString="Valid Connection String;" />
</connectionStrings>
</configuration>
web.config connection string to different database server
The connection I wanted to establish was between different hosting server (one on Windows, another on Linux).
My hosting provider told me it's not possible to connecto from Windows .NET website to Linux MySQL db. Not sure why but not possible.
Would be possible on the same hosting they said.
Thank you for trying to help @T McKeown
Database Connection: Web.config Asp.net
I believe you need this Integrated Security = true into your connection string and it should work. If you don't have this in your connection string you need to specify password and username.
<add name="ConnectionStringMe" connectionString="Data Source=ZOZOW-PC\MSSQLEXPRESS;Database=sample_db;Persist Security Info = True;Integrated Security=True" providerName="System.Data.SqlClient" />
Related Topics
Postgresql Query for Getting N-Level Parent-Child Relation Stored in a Single Table
How to Use a SQL Window Function to Calculate a Percentage of an Aggregate
What Is the Easiest Way to Update an Image Field with the Content of a File
SQL One to One Relationship VS. Single Table
Rows to Comma Separated Values Using Xml Tag
How to Find Top Three Highest Salary in Emp Table in Oracle
SQL Server If Exists Then 1 Else 2
Just Get Column Names from Hive Table
Sqlite: Count Slow on Big Tables
How to Clear All Cached Items in Oracle
Select Latest Records by Datetime Field
How to Select All Columns, and a Count(*) in the Same Query
Trying to Flatten Rows into Columns
How to Select Top X But Still Get a Count of the Whole Query
Find Groups with Matching Rows