Connecting Visual Basic Express 2005 to SQL Server Express 2005 on the same machine

Is this possible? If so can someone give simple instructions on how to do so. I want to be able to access a database I created in SQL server express, from VB express, by adding a connection in the Database Explorer

I have enabled the protocols in SQL server, have kept the default instance, and am using .sqlexpress in the connection box in VB.

I would also like to import an Access database into SQL Server Express but I want to get the connection sorted out first.

I am using Windows XP Pro and SQL Server is on Windows Authentication.

The error I get is a bit long winded but here goes -

Unable to open the physical file <path\FirstSQLdb.mdf. Operating system error 32. "32(error not found)"
An attempt to attach an auto named database for file c:\program files\microsoft sql etc \firstsqldb.mdf failed. A database with the same name exists or specified file cannot be opened or it is located on UNC share.

Any help would be greatly appreciated

Thank you

[1229 byte] By [zzdave] at [2008-1-10]
# 1

Hi,

I have the soluion for it as i am using it in my PC .

First attach the database file to SQL Server 2005 Express by Dos Prompt.

C:\Documents and Settings\Rajeev>sqlcmd -S Rajeev\SQLExpress

1> USE [master]

2> GO

Changed database context to 'master'.

1> CREATE DATABASE [RPSPPGDB] ON

2> (FILENAME ='C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\RPSPPGDB

_DATA.mdf'),(FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\DAT

A\RPSPPGDB_DATA_LOG.ldf') FOR ATTACH;

3> GO

1> select name from sys.databases

2> go

name

--

master

tempdb

model

msdb

RPSPPGDB

(5 rows affected)

1>Exit

Now your database file is attached .

Then

Shared Function GetDataConnection(Optional ByVal which As String = "main") As SqlConnection

Dim strConnectionString As String

strConnectionString = "Network Library=DBNMPNTW;Data Source=RAJEEV\SQLEXPRESS;Initial Catalog=rpsppgdb;"

Dim sql_cn As New SqlConnection(strConnectionString)

sql_cn.Open()

Return sql_cn

End Function

just do it in your program and check it works or not.

Rajeev Aswal

RajeevAswal at 2007-10-3 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 2

zzdave wrote:

I want to be able to access a database I created in SQL server express, from VB express, by adding a connection in the Database Explorer

I have enabled the protocols in SQL server, have kept the default instance, and am using .sqlexpress in the connection box in VB.

The error I get is a bit long winded but here goes
Unable to open the physical file <path\FirstSQLdb.mdf. Operating system error 32. "32(error not found)"
An attempt to attach an auto named database for file c:\program files\microsoft sql etc \firstsqldb.mdf failed. A database with the same name exists or specified file cannot be opened or it is located on UNC share.

Hi zzdave,

Welcome to MSDN forums!

I reproduce the error when I create just a SQL Server databse and then connect it in the Database Explorer. If you are in the same case, you need to first detach the database in SQL Server Management Studio, then the error will disappear when you connect to it again in Database Explorer. Moreover, even if you attach the database again, the error will not still occur. Please take a try.

Here is Microsoft SQL Server Management Studio Express in the official website.

http://www.microsoft.com/downloads/details.aspx?FamilyID=c243a5ae-4bd1-4e3d-94b8-5a0f62bf7796&DisplayLang=en

By the way, here are various connection strings to SQL Server 2005, they are may be helpful to you.

http://www.connectionstrings.com/?carrier=sqlserver2005

Thank you Rajeev for your help and active participation in MSDN forums!

Best regards,

Martin Xie

MartinXie-MSFT at 2007-10-3 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 3

Hi zzdave,

If certain post is not helpful to you, please unmark it.

It would be appreciated that if you have solved the issue and share the solution here so that the answer can be found and used by other community members having similar questions.

If you still have any doubt and concern about this issue, please let me know.

Thanks,

Martin

MartinXie-MSFT at 2007-10-3 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...