A little help for connecting to a database

  • Hi,

    I am having trouble connecting to my database using my Microsoft Visual C#2008 IDE.

    I can define a connection string and connect to my database programmatically using the classes in System.Data.SqlClient. This is the code snippet that I use:

    SqlConnection myConnection = new SqlConnection(connectionString)

    SqlCommand myCommand = myConnection.CreateCommand();

    myCommand.CommandText = " My SQL query";

    myConnection.Open();

    SqlAdapter mySqlAdapter = new SqlAdapter(myCommand.CommandText,myConnection)

    mySqlAdapter.Fill(myDataset,"tables")

    Even though all that works sweet, I cant do the same from my IDE.

    As explained in most of the references I start by selecting connect to a database under the tool tab. I have a local instance of a SQL server in my machine. I could browse onto the data folder of the server but when I try to open the .mdf file it gives me the following warning message:

    "The file is in use. Enter a new name or close the file that is open in another program"

    I tried closing my SSMS but obviously that did help. Where else could my database file be open.

    Iam also having problems connecting to a network SQL server. I could connect to it as explained before.

    But in this case I dont have the option of browsing to the DATA folder as before. How do I populate my dataset using visual studio IDE in this case.

  • You don't use the data file, you specify a connection to the Server.

    Open View -> Server Explorer.

    Right click on Data Connections -> Add Connection.

    Select Microsoft SQL Server database, click Continue, then fill out the connection information box.

    The file is open because it is attached to the SQL Server Instance directly.

    Jonathan Kehayias | Principal Consultant | MCM: SQL Server 2008
    My Blog | Twitter | MVP Profile
    Training | Consulting | Become a SQLskills Insider
    Troubleshooting SQL Server: A Guide for Accidental DBAs[/url]

  • Yes I tried that. But I only have Microsoft SQL Server Compact 3.5 and not Microsoft SQL Server in the Data source list box. The other options I have are Microsoft Access database file and Microsoft SQL Server Database file.

    What am I missing??

  • A full version of Visual Studio? I didn't catch that it was the Visual C# 2008. I am assuming Express Edition? I am not sure about the Express Editions of Visual Studio and if you can do it with designers like you would in the full version

    Jonathan Kehayias | Principal Consultant | MCM: SQL Server 2008
    My Blog | Twitter | MVP Profile
    Training | Consulting | Become a SQLskills Insider
    Troubleshooting SQL Server: A Guide for Accidental DBAs[/url]

  • Yes, I am using C#2008 express edition. But I dont think that Microsoft will cut down on such a prominent feature in express editions.

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply