Accessing an SQL Server database file

G

Guest

My program has to access an SQL Server DB file that is in the same folder as
the executable. i dont want to setup a server. i was able to do this on my
own computer, the program would access any sql server db file without it
being attached to the server. When i take it to another system it fails to
run.
All systems have SQL Server Express and SQL Server 2005 installed.

Can someone please direct me through the steps to fix this problem?

There are 2 errors (on different pcs) i get when i try to access the db one
is:

Error 1

Generating user instances in SQL Server is disabled. Use sp_configure 'user
instances enabled' to generate user instances.


---------------------------
Error 2
---------------------------
System.Data.SqlClient.SqlException: An error has occurred while establishing
a connection to the server. When connecting to SQL Server 2005, this failure
may be caused by the fact that under the default settings SQL Server does not
allow remote connections. (provider: SQL Network Interfaces, error: 26 -
Error Locating Server/Instance Specified)

at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException
exception, Boolean breakConnection)

at
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject
stateObj)

at System.Data.SqlClient.TdsParser.Connect(Boolean& useFailoverPartner,
Boolean& failoverDemandDone, String host, String failoverPartner, String
protocol, SqlInternalConnectionTds connHandler, Int64 timerExpire, Boolean
encrypt, Boolean trustServerCert, Boolean integratedSecurity, SqlConnection
owningObject, Boolean aliasLookup)

at
System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection
owningObject, SqlConnectionString connectionOptions, String newPassword,
Boolean redirectedUserInstance)

at
System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity
identity, SqlConnectionString connectionOptions, Object providerInfo, String
newPassword, SqlConnection owningObject, Boolean redirectedUserInstance)

at
System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions
options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection
owningConnection)

at
System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection, DbConnectionPool pool, DbConnectionOptions options)

at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection
owningObject)

at
System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection
owningObject)

at System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection
owningObject)

at
System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection
owningConnection)

at
System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection
outerConnection, DbConnectionFactory connectionFactory)

at System.Data.SqlClient.SqlConnection.Open()

at DBManager.SqlDBManager.ConnectToDB() in C:\Documents and
Settings\Alessandro Del Piero\My Documents\Visual Studio
2005\Projects\DBManager\DBManager\SqlDBManager.cs:line 53

at DBModule.Form1.button2_Click(Object sender, EventArgs e) in
G:\DBModule\DBModule\Form1.cs:line 205
 
O

Otis Mukinfus

On Sat, 11 Feb 2006 03:09:27 -0800, AlexDP

[snip]

If you are using a provider (Internet) you will have to get the
provider to make the settings recommended in the second message.

If you are on a local network you will need to get the Network staff
to make the settings for you.

Otis Mukinfus
http://www.otismukinfus.com
http://www.tomchilders.com
 
G

Guest

Do you mind explaining to me how the connection string written their works?
because i read through it and then tried to use that connection string (of
course with my own db name) but the string is not a valid C# string, i mean
|DataDirectory| as its mentioned in the description is a keyword but its just
hanging there outside the qoutes and of course it didnt compile as it was, i
tried some other combinations, they didnt work either, so can u please either
tell me the correct connection string, or give me an example?

Nicholas Paldino said:
Alex,

Check this link, it will tell you how to create a connection to SQL
Server and attach to a database file:

http://msdn2.microsoft.com/en-us/library/ms165716.aspx

Hope this helps.
 
A

A.Kahtava

I'm not sure where your problem is, but here are a couple pointers..
The string you are referring to should be a valid C# string..

"|DataDirectory|" should be replaced with your local database
directory.

The @ character proceeding the string means the string should be taken
literally.

The following string:
@"Data Source='.\SQLExpress'; Initial Catalog=; Integrated
Security=true; AttachDBFileName='" + |DataDirectory| + @"\MyDb.mdf'";

is equivalent to:
""Data Source='.\SQLExpress'; Initial Catalog=; Integrated
Security=true; AttachDBFileName='"" + |DataDirectory| + ""\MyDb.mdf'"";

A guide to accessing SQL:
http://www.codeproject.com/cs/database/sql_in_csharp.asp

Hope that helps,

-A.Kahtava
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top