Click here to Skip to main content
15,892,643 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I hv created database in sql server 2008.

now, i want to attach that database in visual studio 2008.

plz write ConnectionString and web.config connection.

Thanks.
Posted
Comments
Shubh Agrahari 25-Feb-13 1:54am    
dude for this 1st you have to try on google....but still i am working on your solution...just wait
harshadcse 25-Feb-13 4:58am    
no...why???
Shubh Agrahari 25-Feb-13 5:00am    
nothing that make you more familiar with it......its okk because now you got your answer
harshadcse 25-Feb-13 6:11am    
yeah,,,,i got it.
Thanks :P
Shubh Agrahari 25-Feb-13 6:13am    
anytime buddy...

hi,

Write in web.config file.

if sql authentication :
XML
<connectionStrings>
<add name="myConnectionString" Data Source=serverName;Initial Catalog=myDb;uid=myUser;password=myPass;" />
</connectionStrings>


XML
if windows authentication :
<connectionstrings>
<add name="myConnectionString" connectionstring="Data Source=serverName;Initial Catalog=myDb; Integrated Security=True" />
</connectionstrings>

getting connection string from web.config file.
need to add following namespace

using System.Configuration;

and connect the open the database connection is,

SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString);


regards,
Prakash.T
 
Share this answer
 
two way you can do it:

1: using Appsetting section
HTML
<appsettings><add key="APPConnectionString" value="Data Source=ABC-PC\SQLEXPRESS; Initial Catalog=TEST;Persist Security Info=true;User ID=sa; password=SA" />  </appsettings>

to access this from .CS File try this :
C#
private string sqlConnectionString = null;
   private SqlConnection sqlConn = null;
 sqlConnectionString = ConfigurationManager.AppSettings["APPConnectionString"];
 sqlConn = new SqlConnection(sqlConnectionString);

2: Using connection string Section:

XML
<connectionStrings>
        <add name="test" connectionString="Data Source=ABC-PC\SQLEXPRESS; Initial Catalog=TEST;Persist Security Info=true;User ID=sa; password=sa" providerName="System.Data.SqlClient"/>
    </connectionStrings>



to access this from .CS File try this :

C#
private string sqlConnectionString = null;
    private SqlConnection sqlConn = null; 
        sqlConnectionString = ConfigurationManager.ConnectionStrings["test"].ConnectionString;
        sqlConn = new SqlConnection(sqlConnectionString);
 
Share this answer
 
Standard Security
C#
Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;


Trusted Connection
C#
Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;



Connection to a SQL Server instance
C#
Server=myServerName\myInstanceName;Database=myDataBase;User Id=myUsername;
Password=myPassword;
 
Share this answer
 
hi buddy..

use the connectionStrings section in web.config.

XML
<connectionstrings>
<add name="myConnectionString" connectionstring="server=localhost;database=yourdbname;User Id=sqlid;password=sqlPass;" />
</connectionstrings>


To read the connection string into your code, use the ConfigurationManager class.

C#
string connStr = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;


Remember to add reference to web page add the namespace using System.Data.SqlClient; in your code to get access to the ConfigurationManager class.

Thanks
Happy to help...
 
Share this answer
 
Comments
Shubh Agrahari 25-Feb-13 7:30am    
thanks..
harshadcse 5-Mar-13 0:41am    
Thank u so much
Shubh Agrahari 5-Mar-13 9:01am    
ya always be ready to help...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900