How to I create database with C#? And how to I set Connection String in C# with SQL Server 2012 ? . In this article I will explain how to write connection strings. I have one web application that contains many pages and each page contains relationship with database connection to get data from database and display it on page because of that I need to write database connections for each page to interact with database. Now the server name or credentials of database server has changed in that situation it will create problem because we need to modify the database connections of each page using asp.net.

Two Type of SqlConnection String First for sql file and Second for sql server you have to almost use 2nd type. Passing IP address of Database server , database name , User id & password.

write the following code in code behind :

Standard Connection

using System.Data.SqlClient;
SqlConnection conn = new SqlDbConnection();

Server name or ip address is come there , Initial Catalog for Database name;UserName for user id and secret for password // is sql user id and password by defaul sql user is 'UserName' and password you should be set it

conn.ConnectionString ="Data Source=ServerName; Initial Catalog=DataBaseName; User id=UserName; Password=Secret;";
conn.Open();

Or u can try this one for Trusted Connection :

conn.ConnectionString ="Data Source=ServerName; Initial Catalog=DataBaseName; User id=UserName; Password=Secret;";conn.Open();using System.Data.SqlClient;SqlConnection conn = new SqlConnection();conn.ConnectionString = "Data Source=ServerName; Initial Catalog=DataBaseName; Integrated Security=SSPI;";
conn.Open();