[RESOLVED] database connection problem
Hi, I'm trying to connect a Linq-to-SQL context to a database with a relative path using DataDirectory but I get the following error:
"You need to specify the full path to an executable file"
My data access class constructor in a separate DLL looks like this:
Code:
Private Sub New()
dc = New MilkywayDataContext()
End Sub
In the app.config folder for the same DLL I have this:
Code:
<connectionStrings>
<add name="MilkywayDataContext"
connectionString="data source=(LocalDB)\v11.0; AttachDbFilename =|DataDirectory|\MilkywayDB.mdf;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
And in the web.config for the main project, I have the exact same.
I don't know why it's not working as I have another project which uses the same method, albeit as an Entity Framework Model, and it works fine.
Any help will be greatly appreciated!
Re: database connection problem
I should have pointed out that the New MilkywayDataContext() contained a ConnectionString parameter from My.Settings previously. I've deleted this setting and the compiler now points to this code:
Code:
Imports System.Linq.Expressions
Imports System.Reflection
<Global.System.Data.Linq.Mapping.DatabaseAttribute(Name:="MilkywayDB")> _
Partial Public Class MilkywayDataContext
Inherits System.Data.Linq.DataContext
Private Shared mappingSource As System.Data.Linq.Mapping.MappingSource = New AttributeMappingSource()
#Region "Extensibility Method Definitions"
Partial Private Sub OnCreated()
End Sub
Partial Private Sub InsertBatch(instance As Batch)
End Sub
Partial Private Sub UpdateBatch(instance As Batch)
End Sub
Partial Private Sub DeleteBatch(instance As Batch)
End Sub
Partial Private Sub InsertUserRole(instance As UserRole)
End Sub
Partial Private Sub UpdateUserRole(instance As UserRole)
End Sub
Partial Private Sub DeleteUserRole(instance As UserRole)
End Sub
Partial Private Sub InsertImage(instance As Image)
End Sub
Partial Private Sub UpdateImage(instance As Image)
End Sub
Partial Private Sub DeleteImage(instance As Image)
End Sub
Partial Private Sub InsertIngredient(instance As Ingredient)
End Sub
Partial Private Sub UpdateIngredient(instance As Ingredient)
End Sub
Partial Private Sub DeleteIngredient(instance As Ingredient)
End Sub
Partial Private Sub InsertProduct(instance As Product)
End Sub
Partial Private Sub UpdateProduct(instance As Product)
End Sub
Partial Private Sub DeleteProduct(instance As Product)
End Sub
Partial Private Sub InsertProductIngredient(instance As ProductIngredient)
End Sub
Partial Private Sub UpdateProductIngredient(instance As ProductIngredient)
End Sub
Partial Private Sub DeleteProductIngredient(instance As ProductIngredient)
End Sub
Partial Private Sub InsertStatus(instance As Status)
End Sub
Partial Private Sub UpdateStatus(instance As Status)
End Sub
Partial Private Sub DeleteStatus(instance As Status)
End Sub
Partial Private Sub InsertUser(instance As User)
End Sub
Partial Private Sub UpdateUser(instance As User)
End Sub
Partial Private Sub DeleteUser(instance As User)
End Sub
#End Region
Public Sub New()
MyBase.New(Global.MilkywayDLL.My.MySettings.Default.MilkywayDBConnectionString, mappingSource)
OnCreated
End Sub
How can I change the invocation to the parent constructor so it works with the connections string in the config file?
Re: database connection problem
Well the error message looks clear enough to me! Short of rewriting VB itself I'm not sure what we can do other than suggest you do as you're told.
Quote:
albeit as an Entity Framework Model
So, no difference there then?
Re: database connection problem
How exactly did you generate that connection string? I just added a Service-based Database item to a project in VS 2012 and got something similar except that the name looked like this: "WindowsApplication1.My.MySettings.Database1ConnectionString".
Also, are you sure that it's the connection string that's the issue? The fact that the error message says:
Quote:
You need to specify the full path to an executable file
suggests otherwise.
Re: database connection problem
Quote:
Originally Posted by
garry79
I should have pointed out that the New MilkywayDataContext() contained a ConnectionString parameter from My.Settings previously. I've deleted this setting and the compiler now points to this code:
Code:
Imports System.Linq.Expressions
Imports System.Reflection
<Global.System.Data.Linq.Mapping.DatabaseAttribute(Name:="MilkywayDB")> _
Partial Public Class MilkywayDataContext
Inherits System.Data.Linq.DataContext
Private Shared mappingSource As System.Data.Linq.Mapping.MappingSource = New AttributeMappingSource()
#Region "Extensibility Method Definitions"
Partial Private Sub OnCreated()
End Sub
Partial Private Sub InsertBatch(instance As Batch)
End Sub
Partial Private Sub UpdateBatch(instance As Batch)
End Sub
Partial Private Sub DeleteBatch(instance As Batch)
End Sub
Partial Private Sub InsertUserRole(instance As UserRole)
End Sub
Partial Private Sub UpdateUserRole(instance As UserRole)
End Sub
Partial Private Sub DeleteUserRole(instance As UserRole)
End Sub
Partial Private Sub InsertImage(instance As Image)
End Sub
Partial Private Sub UpdateImage(instance As Image)
End Sub
Partial Private Sub DeleteImage(instance As Image)
End Sub
Partial Private Sub InsertIngredient(instance As Ingredient)
End Sub
Partial Private Sub UpdateIngredient(instance As Ingredient)
End Sub
Partial Private Sub DeleteIngredient(instance As Ingredient)
End Sub
Partial Private Sub InsertProduct(instance As Product)
End Sub
Partial Private Sub UpdateProduct(instance As Product)
End Sub
Partial Private Sub DeleteProduct(instance As Product)
End Sub
Partial Private Sub InsertProductIngredient(instance As ProductIngredient)
End Sub
Partial Private Sub UpdateProductIngredient(instance As ProductIngredient)
End Sub
Partial Private Sub DeleteProductIngredient(instance As ProductIngredient)
End Sub
Partial Private Sub InsertStatus(instance As Status)
End Sub
Partial Private Sub UpdateStatus(instance As Status)
End Sub
Partial Private Sub DeleteStatus(instance As Status)
End Sub
Partial Private Sub InsertUser(instance As User)
End Sub
Partial Private Sub UpdateUser(instance As User)
End Sub
Partial Private Sub DeleteUser(instance As User)
End Sub
#End Region
Public Sub New()
MyBase.New(Global.MilkywayDLL.My.MySettings.Default.MilkywayDBConnectionString, mappingSource)
OnCreated
End Sub
How can I change the invocation to the parent constructor so it works with the connections string in the config file?
If you open your DBML file in the designer then open the Properties window you will see the properties for the DataContext, including the Connection property.
Re: database connection problem
Quote:
|DataDirectory| (enclosed in pipe symbols) is a substitution string that indicates the path to the database. It eliminates the need to hard-code the full path which leads to several problems as the full path to the database could be serialized in different places. DataDirectory also makes it easy to share a project and also to deploy an application.
For example, instead of having the following connection string:
"Data Source= c:\program files\MyApp\Mydb.sdf"
Using DataDirectory, you can have the following connection string:
“Data Source = |DataDirectory|\Mydb.sdf”
To set the DataDirectory property, call the AppDomain.SetData method. If you do not set the DataDirectory property, the following default rules will be applied to access the database folder:
•For applications that are put in a folder on the user's computer, the database folder uses the application folder.
•For applications that are running under ClickOnce, the database folder uses the specific data folder that is created
See above quote
Re: database connection problem
Thanks for your comments!
I've changed the constructor to this, to try to get the path from the web config file:
Code:
Public Sub New()
'MyBase.New(Global.MilkywayDLL.My.MySettings.Default.MilkywayDBConnectionString, mappingSource)
MyBase.New(WebConfigurationManager.ConnectionStrings("MilkywayDataContext").ToString())
OnCreated()
End Sub
And I'm getting the same error again :(
Re: database connection problem
I don't remember how I generated the connection string. The error continues to pop up. It's so annoying because it doesn't point to the code. How am I supposed to find the error if it doesn't point to it? Aghhh!
Re: database connection problem
Quote:
Originally Posted by
garry79
I don't remember how I generated the connection string. The error continues to pop up. It's so annoying because it doesn't point to the code. How am I supposed to find the error if it doesn't point to it? Aghhh!
By reading the stack trace of the exception.
Re: database connection problem
I've recreated the DAL and it works now
Thanks