tags:

views:

329

answers:

3

Is it possible to access a Visual Studio 2008-created server-based database from a VB-6 application? If not, what is the best way to share a database between a VB-6 application and a C# application?

+1  A: 

If you are referring to the SQL Server Express database that comes with Visual Studio 2008, you can access that with plain old ADO.

The connection strings for doing this are at http://connectionstrings.com/sql-server-2005. The most appropriate one is probably this one:

Driver={SQL Native Client};Server=myServerAddress;Database=myDataBase;
Uid=myUsername;Pwd=myPassword;

For SQL Server Express (which is what comes with VS2008), don't miss the server name syntax Servername\SQLEXPRESS where you substitute Servername with the name of the computer where the SQL Server 2005 Express installation resides.

The reference for programming against a SQL Server database using ADO is here: http://msdn.microsoft.com/en-us/library/aa905875(SQL.80).aspx

Example for connecting to the SQL Server database is below (untested). Note that this one uses SQL Server authentication rather than Windows Authentication. Check your connection strings on http://www.connectionstrings.com. If SQL Native Client doesn't work, try one of the others.

' Initialize variables.
Dim cn As New ADODB.Connection
Dim connectionString As String

' Specify the OLE DB provider.
cn.Provider = "{SQL Native Client}"

' Specify connection string on Open method.
connectionString = Server=myMachineName\SQLEXPRESS;Database=myDataBase; _
   Uid=myUsername;Pwd=myPassword;
cn.Open connectionString
Robert Harvey
I need to deploy to other machines, the vb6 application must be able to access the database used by the C# application. I am not able to see the SQL service DB I created in my 2008 project. Any suggestions?
mikeh
Are you getting an error message?
Robert Harvey
No error message, just not able to see the database from Visual Studio 6, and not sure how to connect to the mdf file from VS 6, since the path in Visual Studio 2008 is displaying as being in the working project directory and not the final install directory. Or am I looking in the wrong place?
mikeh
If you use connection strings to get at the database you are not going to see much until you try to connect. If you are using Control Panel to create a DSN there may be other issues...but I favor using a connection string and attempting to connect that way. See my edit in the answer.
Robert Harvey
A: 

I just added a "Service-based database" to a project. I go the following in my app.config, in the configuratonStrings element:

<add name="SerializeToSql.Properties.Settings.Database1ConnectionString"
    connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True"
    providerName="System.Data.SqlClient" />

I don't know if ADO has user instance support. if it does, then you may be able to create an equivalent connection string for ADO to use. Otherwise, you'll need to just create a "normal" database in SQL Server Express. You should be able to use that from ADO.

John Saunders
A: 

In order for other machines to see your SQL Express instance, you need to make sure it allows remote connections, the SQL Browser service is turned on, and is allowed through the firewall.

  1. Fire up the SQL Server 2005 Surface Area Configuration tool: All Programs->Microsoft SQL Server 2005->Configuration Tools->SQL Server Surface Area Configuration.
  2. Click Surface Area Configuration for Services and Connections
  3. Click the 'Remote Connections' node and select 'Local and remote connections'. If you want to refer to the server as ServerName\MSSQLSERVER, you'll have to user named pips or TCP/IP and named pipes.
  4. Click the SQL Server Browser node. Change the Startup type to Automatic and start the service.

For the firewall, make sure you make an exception for the SQL Server program (C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Binn\sqlservr.exe) and for the SQL Server Browser program (c:\Program Files\Microsoft SQL Server\90\Shared\sqlbrowser.exe)

C-Pound Guru
Thanks. I don't need other machines to see the instance, I need another program (vb6) on the same machine to see the database when the 2 pieces of software are installed on other machines.
mikeh