Connect to SQL SERVER EXPRESS only by code from VB EXPRESS

Dear Friends,

I know that by VB EXPRESS can connect only by code to database in SQL SERVER EXPRESS.
Now I have try with the code below but obtain this error:


Answer this question

Connect to SQL SERVER EXPRESS only by code from VB EXPRESS

  • Puneetm

    Public Const connectionString As String = "Data Source=CASA\SQLEXPRESS;Initial Catalog=Test;Integrated Security=True;"

    Is the machine you are running this VB Code on called CASA or is CASA a different remote machine.

    This connection string is a SQL Server Connection string and not a SQL Express One.

    The SQL Express connections normally will be something like

    "Server=.\SQLExpress;AttachDbFilename=c:\asd\qwe\mydbfile.mdf;Database=dbname;Database=dbname;Trusted_Connection=Yes;"
    - or -
    "Server=.\SQLExpress;AttachDbFilename=|DataDirectory|mydbfile.mdf;Database=dbname;Trusted_Connection=Yes;"
    (use |DataDirectory| when your database file resides in the data directory)

    The fact that SQL Express is a local database solution also normally means that it would have a server parameter set as .\SQLExpress rather than CASA\SQLExpress which may imply you are trying to run it on a remote machine.


  • Omen

    Yes I can connect very well with SQL Server Management
  • Mike Williams28205


    SQL Express doesn't allow you to connect to database files over a file share. Unlike file-based databases like Access, SQL Express runs as a service that attaches to the database and then works pretty much like any other version of SQL Server. If you want to enable multiple users , you'll need to setup the database to be accessed remotely.

    First, enable remote database connections to the machine with SQL Express. There are some good instructions here for doing that:

    http://blogs.msdn.com/sqlexpress/archive/2005/05/05/415084.aspx

    Once you have a machine setup with remote access enabled, you'll also need to attach your local database file. You'll want to attach to the main instance. The easiest way to do that is to download sseutil from http://www.microsoft.com/downloads/details.aspx FamilyID=fa87e828-173f-472e-a85c-27ed01cf6b02&DisplayLang=en and then run the following command:

    sseutil -m -a <PathToYourDatabaseFile> <DatabaseName>

    DatabaseName can be any valid name of your choosing. For example, if you were attaching a Northwind database from d:\Northwind.mdf the command would look like:

    sseutil -m -a D:\Northwind.mdf Northwind

    After you have attached the database, you will need to alter the connection string in your application to use the remote server and database instance instead of a file path. By default the database instance name for SQL Express is "SQLEXPRESS". Here's the format of a minimal connection string:

    Data Source=<ServerName>\SQLEXPRESS;Initial Catalog=<DatabaseName>;Integrated Security=True

    DatabaseName is whatever you chose when attaching the database. Extending the example above, if you have a server named "MyDatabaseServer" you would use the following connection string:

    Data Source=MyDatabaseServer\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True

    After that you should be able to connect multiple concurrent users. I hope this helps.


  • emilychou

    Dear Spotty

    I have already written successfull a code for connection to a "local database file" .mdf

    What I would do now is create a database in a so called "remote server" (also if it is on the same computer), in other words, in my istance CASA of SQL Server Express.

    Now l can do it only by SQL Server Management.

    I know that VB Express can do it by code and not by drag-drop mode.

    Thanks


  • markvanh

    Perhaps your login to the server did actually fail. Can you connect using SQL Server Management studio

  • Connect to SQL SERVER EXPRESS only by code from VB EXPRESS