NoLuckCollector
NoLuckCollector

Reputation: 65

How to open a connection on the back end in C# to a local database

I had a database server for school and since the semester is over I no longer am on this server. I want to take all my projects and put them on my website I've been working on. The problem is they have the school server connection. On the back end when I want to open a connection to my local database how would I do this? My connection looks like this

SqlConnection con = new SqlConnection("Data Source=LocalServerName;");

I get an error about the user, but my username for my local connection has a \ in it and that's an invalid key so I can't put my username in

Upvotes: 0

Views: 371

Answers (1)

Adam
Adam

Reputation: 4188

Try this....

string connectionString = "Integrated Security=SSPI;Initial Catalog=master;Data Source=(local)"
SqlConnection con = new SqlConnection(connectionString);

You can change the value for Initial Catalog from "master" to the name of your database. I'm assuming you're using integrated security (you login to SQL Server with your Windows account).

If your login failed, look in SQL Server's error log for a failed login attempt. The error will give you more details about why the login attempt failed.

If you're logging in using a domain account - a.k.a., Windows authentication, a.k.a., Integrated Security - you'll include the following in your connection string:

Integrated Security=SSPI;

If you are connecting with a SQL Server username (username and password maintained by SQL Server and NOT by Windows or Active Directory). You'll use the following:

Persist Security Info=True;User ID=<username>;Password=<password>;

If you're not 100% sure how to create your connection string, try this trick:

  1. Create a text file on your desktop named "test.udl"
  2. Once created, double click on that file.
  3. You can set all the properties of your connection string here.
  4. When you're done, hit OK.
  5. Then open the file in a text editor (e.g., notepad).
  6. You'll see a connection string configured with the options you previously set. Copy and paste.

Upvotes: 1

Related Questions