sarah sh
sarah sh

Reputation: 315

sqlserver 2008 with visual studio 2010

I have installed the vs2010 and now I am using the "SQLEXPRESS" for connecting to a database, but now I want to use the sqlserver 2008 directy not SQLEXPRESS; for more explanation:

The sql express has this connection string(for example):(and I dont want to use it!)

connection.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\EHSAN\Documents\Visual Studio 2010\Projects\hokm\hokm\App_Data\database1.mdf;Integrated Security=True;User Instance=True";

But I want to use this command(for ex):

Server=localhost;Database=xxx;User " +"ID=xx;Password=xxxx;

And I dont know how should I make a database witch want this connection string!

Upvotes: 0

Views: 1937

Answers (5)

Pankaj
Pankaj

Reputation: 695

Try the following:

connection.ConnectionString = @"Server=yourservername;Database=yourdatabasename;UID=yourusername;Password=youruserpassword";

Upvotes: 0

arunlalam
arunlalam

Reputation: 1848

The best way to add connection string in ASP .NET is to add it to web.config. This way if you change your server name or something, you do not need to go and change the connection string in all the .cs files.

In your web.config you ll add

<connectionStrings>    
        <add name="ConnectionStringName" connectionString="server=ServerName;database=DBName;User ID=UserName;Password=YourPassword;" providerName="System.Data.SqlClient" />
</connectionStrings>

To access the connection string from your C# code you ll use

System.Configuration.ConfigurationManager.
ConnectionStrings["ConnectionStringName"].ConnectionString;

If you want to use Windows Authentication you ll change you conenction string to connectionString="server=ServerName;database=DBName;Integrated Security=True;"

Upvotes: 5

Obama
Obama

Reputation: 2612

Put this at the top of your code:

using System.Web.Configruation;

Put this in Web.Config:

<connectionStrings >
<add name="myConnectionString" connectionString="Server=myServerAddress;Database=myDataBase;User ID=myUsername;Password=myPassword;Trusted_Connection=False;" providerName="System.Data.SqlClient"/>
 </connectionStrings>

and where you want to setup the connection variable:

SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["myConnectionString"].ToString());

Hope this Helps!

Upvotes: 2

Ramesh Rajendran
Ramesh Rajendran

Reputation: 38713

Must installed sql server 2008 version and Try this

connection.ConnectionString = @"Data Source=.\SQLEXPRESS;Database="";User ID=YourServerUserName;Password=Password;"

Upvotes: 0

Jonathan
Jonathan

Reputation: 5028

It should be as simple as:

"Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;"

Upvotes: 0

Related Questions