0
votes

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!

5
You will need to install SQL 2008 either on your local machine or or a server if it doesn't already exist. Also, poor practice to include usernames and passwords in your question. - andleer
For the record those username and password credentials are going to exist in the edit history of this post forever and ever. - djechlin

5 Answers

5
votes

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;"

2
votes

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!

0
votes

Try the following:

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

It should be as simple as:

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

Must installed sql server 2008 version and Try this

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