469,270 Members | 1,152 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,270 developers. It's quick & easy.

Accessing an SQL Server database file

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.OnErro r(SqlException
exception, Boolean breakConnection)

at
System.Data.SqlClient.TdsParser.ThrowExceptionAndW arning(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.Ope nLoginEnlist(SqlConnection
owningObject, SqlConnectionString connectionOptions, String newPassword,
Boolean redirectedUserInstance)

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

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

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

at System.Data.ProviderBase.DbConnectionPool.CreateOb ject(DbConnection
owningObject)

at
System.Data.ProviderBase.DbConnectionPool.UserCrea teRequest(DbConnection
owningObject)

at System.Data.ProviderBase.DbConnectionPool.GetConne ction(DbConnection
owningObject)

at
System.Data.ProviderBase.DbConnectionFactory.GetCo nnection(DbConnection
owningConnection)

at
System.Data.ProviderBase.DbConnectionClosed.OpenCo nnection(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
---------------------------
OK
---------------------------

Feb 11 '06 #1
4 23369
On Sat, 11 Feb 2006 03:09:27 -0800, AlexDP
<Al****@discussions.microsoft.com> wrote:

[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
Feb 11 '06 #2
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"AlexDP" <Al****@discussions.microsoft.com> wrote in message
news:6B**********************************@microsof t.com...
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.OnErro r(SqlException
exception, Boolean breakConnection)

at
System.Data.SqlClient.TdsParser.ThrowExceptionAndW arning(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.Ope nLoginEnlist(SqlConnection
owningObject, SqlConnectionString connectionOptions, String newPassword,
Boolean redirectedUserInstance)

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

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

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

at System.Data.ProviderBase.DbConnectionPool.CreateOb ject(DbConnection
owningObject)

at
System.Data.ProviderBase.DbConnectionPool.UserCrea teRequest(DbConnection
owningObject)

at System.Data.ProviderBase.DbConnectionPool.GetConne ction(DbConnection
owningObject)

at
System.Data.ProviderBase.DbConnectionFactory.GetCo nnection(DbConnection
owningConnection)

at
System.Data.ProviderBase.DbConnectionClosed.OpenCo nnection(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
---------------------------
OK
---------------------------

Feb 11 '06 #3
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 [.NET/C# MVP]" wrote:
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"AlexDP" <Al****@discussions.microsoft.com> wrote in message
news:6B**********************************@microsof t.com...
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.OnErro r(SqlException
exception, Boolean breakConnection)

at
System.Data.SqlClient.TdsParser.ThrowExceptionAndW arning(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.Ope nLoginEnlist(SqlConnection
owningObject, SqlConnectionString connectionOptions, String newPassword,
Boolean redirectedUserInstance)

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

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

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

at System.Data.ProviderBase.DbConnectionPool.CreateOb ject(DbConnection
owningObject)

at
System.Data.ProviderBase.DbConnectionPool.UserCrea teRequest(DbConnection
owningObject)

at System.Data.ProviderBase.DbConnectionPool.GetConne ction(DbConnection
owningObject)

at
System.Data.ProviderBase.DbConnectionFactory.GetCo nnection(DbConnection
owningConnection)

at
System.Data.ProviderBase.DbConnectionClosed.OpenCo nnection(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
---------------------------
OK
---------------------------


Feb 12 '06 #4
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/databa..._in_csharp.asp

Hope that helps,

-A.Kahtava

Feb 14 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Scott Castillo | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.