Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I’m experimenting with a database application and I have run into a snag. I’m writing it in VB.NET 2008.
In the application I have a function that is suppose to dynamically create a connection string for either an MS-Access database or a SQL Server database at runtime.
Connection string builder function:
VB
Public Function cnnDataBase(ByVal strDBType As String, _
                            ByVal strDBName As String, _
                            Optional ByVal strServer As String = "") As String

   Select Case UCase(strDBType)
      Case "OLEDB"
          Dim cnnOLEDB As New OleDbConnectionStringBuilder
          cnnOLEDB.Provider = "Microsoft.Jet.OLEDB.4.0"
          cnnOLEDB.DataSource = strDBName
          Return cnnOLEDB.ConnectionString
      Case "SQL"
          Dim cnnSQL As New SqlConnectionStringBuilder
          'This section is SUPPOST to connect to the
          'AdoStepByStep_Data.MDF SQL Server file, but it fails
          cnnSQL.DataSource = "(local)\SQLEXPRESS" 'strServer
          cnnSQL.InitialCatalog = "AdoStepByStep_Data.MDF"
          cnnSQL.IntegratedSecurity = True
          'This connection string will let the application
          'connect to the AdoStepByStep database file
          'Dim strCnWorks As String = "Data Source=(local)\SQLEXPRESS;" & _
          '                            "Initial Catalog=AdoStepByStep;" & _
          '                            "Integrated Security=True"
          Return cnnSQL.ConnectionString
          'Return strCnWorks
      Case Else
          Return Nothing
      End Select
End Function

Currently I’m hard coding in these values to isolate why it isn’t working. In practice the function receives three parameters; The strDBType variable is a string value indicate what kind of connection I want to build, either a MS-Access(OLEDB) or SQL Server(SQL). The strDBName variable is the file name of the database I want to connect to. I am retrieving this value through the OpenFileDialog class from inside another function. The last variable is the SQL Server name and instance, which is my desktop and SQLEXPRESS.
The OLEDB part works fine, but the SQL Server part is causing me ulcers. After playing with it all day I have made an interesting discovery. The Initial catalog file I was using to test with AdoStepByStep was created with the ConfigDB application stored on the Microsoft Press SampleDBs folder. I don’t know what this application did, but it somehow massaged the AdoStepByStep_Data.MDF primary SQL Server database file into something the connection object recognized. OLEDB side works okay because it uses the Access JET Engine.

When I change the Initial Catalog to AdoStepByStep_Data.MDB the program throws the exception: "Cannot Open 'AdoStepByStep_Data.MDB' requested by login. The Login failed." The only difference I see is that this one is the SQL Server Primary database file and the Initial Catalog that is set to "AdoStepByStep" works. The only thing I can come up with is the AdoStepByStep one was somehow transformed into something usable by the ConnectDB application.

Now the question: What do I have to do so I can access the .MDF file directly so I can create DataSets?

Thank you,
Quecumber256
Posted
Updated 12-Jul-12 2:44am
v3
Comments
[no name] 11-Jul-12 16:36pm    
"it isn’t working"... is not a very good description of a problem. What is it doing instead of "working"? Do you get any errors? Is AdoStepByStep_Data.MDF hosted by SQL Server? Can you connect to AdoStepByStep_Data.MDF using Management Studio?
[no name] 12-Jul-12 9:10am    
Now I see your problem. One of them anyway. AdoStepByStep_Data.MDF is the name of the file and you need to put the name of the database in there (AdoStepByStep).

1 solution

The Code snippet in the question has the answer in it.

As Wes Aday said in the comments:

Change:
cnnSQL.InitialCatalog = "AdoStepByStep_Data.MDF"
To:
cnnSQL.InitialCatalog = "AdoStepByStep"


If this does not solve your problem, please post a response to the suggestions.

As for the question: What do I have to do so I can access the .MDF file directly so I can create DataSets?

You can not access the .MDF file "Directly", you must mount the database to an SQL Server and access the database using a connection string to attach to the SQL server before you can create datasets.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900