3

I have SQL Server 2008 installed with Visual Studio 2010, I have downloaded a project which has database files created in Visual Studio project itself, now I want to connect to that database.

I tried changing the connection string but unable to connect to the database.

Can anyone tell me how to connect to the database, I have SQL Server 2008 (10.0.1600.22) installed on my machine

Updated connection string:

Here is the connection string I am using

Data Source=SQLEXPRESS\;Initial Catalog=INVENTORY;uid=xxx;pwd=xxx

where xxx\xxx is my machine and my installed SQL Server instance name respectively.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Abbas
  • 4,948
  • 31
  • 95
  • 161
  • 1
    Can we see your connectionstring? – Freddie Fabregas Feb 28 '13 at 03:48
  • 1
    What you try and what is a error?.Without proper information no one able to give a proper ans.So plz show your effort. – 4b0 Feb 28 '13 at 03:48
  • Your connection string is wrong it should start with SQLExpress\ after Data Source= – Frazell Thomas Feb 28 '13 at 03:50
  • Gun without a bullet is useless. Alike with Username and Password. – Freddie Fabregas Feb 28 '13 at 03:51
  • Hi guys, i have updated my connection string, can you please let me know is it fine, still i am unable to run my application – Abbas Feb 28 '13 at 03:53
  • what specific error are you receiving. Install sql server management studio (express if you cannot get full) and try to connect using your credentials. – Mike C. Feb 28 '13 at 03:54
  • 1
    should it be `Data Source=machinename\SQLEXPRESS`? – Freddie Fabregas Feb 28 '13 at 03:54
  • You should obfuscate your user name and password; we only need to know where you put it, we don't need to know exactly what it is. – Dour High Arch Feb 28 '13 at 03:54
  • i already have sql server version (10.0.1600.22) installed, do i still need to install sql server express to run my application – Abbas Feb 28 '13 at 03:55
  • 1
    Yes, he should try Data Source=(local)\SQLExpress – Mike C. Feb 28 '13 at 03:55
  • SSMS (mgmt studio) is a tool, it's not necessary but it's helpful to troubleshoot things like this. VS2012 has decent tools where you could get by without, but I'm not sure that 2010 does. – Mike C. Feb 28 '13 at 03:56
  • What are the "database files created in visual studio"? Do they have the filename extension .SDF? Those are SQL CE files, not MS SQL Express. – Dour High Arch Feb 28 '13 at 03:57
  • I think he is referring to DB instances created in VS, for e.g. When using VS Express, it doesn't have the ability to connect to regular SQL Server (limitation on Express). So there is local database **compact Edition**... (which is `.sdf` format) – aspiring Feb 28 '13 at 03:59
  • the files are .mdf and .ldf, just wanted to point here is that i haven't attach those database in sql server yet, i just want to call the .mdf file from within visual studio, because if i deploy this app to my client its not necessary that i have a sql server installed on my client machine. – Abbas Feb 28 '13 at 04:00
  • @Abbas Just to reconfirm, are you sure you are not using `VS Express`? – aspiring Feb 28 '13 at 04:01
  • I am using Visual Studio 2010 Ultimate. – Abbas Feb 28 '13 at 04:02
  • where's .mdf located in your project? – Jobert Enamno Feb 28 '13 at 04:10
  • Server=myServerName\myInstanceName;Database=myDataBase;User Id=myUsername; Password=myPassword; – Habib Zare Feb 28 '13 at 04:30
  • change attachedfilename to your .mdf path : Data Source=.\SQLExpress;Integrated Security=true; AttachDbFilename=C:\MyFolder\MyDataFile.mdf;User Instance=true; – Habib Zare Feb 28 '13 at 04:37
  • do you have only SQL Express installed? Don't you have commercial version? – Jobert Enamno Feb 28 '13 at 04:38
  • i have Commercial Version installed. – Abbas Feb 28 '13 at 04:49
  • You forgot to mention what approach you are using. Are you using EntityFramework or ADO.NET? So do you have EXPRESS and Commercial versions or Commercial only? – Jobert Enamno Feb 28 '13 at 05:02

2 Answers2

5

Are you using C#?

try this:

using namespace System.Data.SqlClient;

SqlConnection con = new SqlConnection("Data source = [ip]; Initial catalog = [db name]; User id = [user name]; password = [password]");

con.Open();

set a breakpoint at con.Open(), if it succeeds and passed this line, that means you have successfully connected to the database.

After that, you may want to execute a SQL Command try this:

    SqlCommand cmd = new SqlCommand("[command]", con);

        // if the command has no return value
        cmd.ExecuteNonQuery();
        //else you might want a Sql data reader to read the return value
        SqlDataReader read = cmd.ExecuteReader();
        while(read.Read())
    {
//do something
    }
Dan
  • 59,490
  • 13
  • 101
  • 110
User2012384
  • 4,769
  • 16
  • 70
  • 106
  • 1
    Good!, but. Is SqlConnection con = ...and not SqlConncetion con = ... and is .ExecuteReader(); and not .ExcuteReader();. Check your typing. – Hernaldo Gonzalez Jun 25 '14 at 13:50
1

To connect to a database file created in SQL Server, open the web.config file in visual studio and do the following:

  1. Open a <connectionString> tag inside <configuration> tag.
  2. Paste the following in between the <connectionString> tags.

<add name="NameofConnectionString" connectionString="server=YourServerInstanceName; database=DatabaseName; integrated security=SSPI" providerName="System.Data.SqlClient"/>
  1. Refresh the server explorer in visual studio to get connected to the database

Hope this helps!

Kartheek N
  • 57
  • 6