Google

Sunday, November 4, 2007

How to connect with SQL database?

For connecting to SQL Database, you can directly write a connection string in the C# code like
SQLConnection sConn = new SQLConnection();

sConn="Data Source=xyz;Initial Catalog=myDtbase;User ID=sl;Password=sa"

But the proper way to do it is by adding the connection string in a web.config, for example
in
<configuration>

<appSettings>

<add key="conn1String" value="Data Source=xyz;Initial Catalog=myDtbase;User ID=sl;Password=sa"/> </appSettings>

</configuration> <appsettings>
<appsettings>
<add key="conn1String" value="Data Source=xyz;Initial Catalog=myDtbase;User ID=sl;Password=sa">
</appsettings>
<add key="conn1String" value="Data Source=xyz;Initial Catalog=myDtbase;User ID=sl;Password=sa">
</configuration></appsettings></configuration>

In C# code , just define

string connString = ConfigurationSettings.AppSettings["conn1String"];

sConn =connString;

Data Source : has to be the complete machine name\SQLExpress

Initial Catalog : has to be the database name

User ID and Password : If you use other than windows authentication
If it is windows authentication, just give Integrated Security=SSPI;

No comments: