Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I' ve been writing a setup with the following code to attach a database to sql server 2008 using SMO in VB.Net 2010:

Dim srv As New Smo.Server()
Dim strDataFile As String = "Application.StartupPath + \dbPatientLabWork.mdf"
Dim logDataFile As String = "Application.StartupPath + \dbPatientLabWork_log.ldf"
Dim strCopyToFolderMdf As String = "C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\dbPatientLabWork.mdf"
FileCopy(strDataFile, strCopyToFolderMdf)
Dim strCopyToFolderLdf As String = "C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\dbPatientLabWork_log.ldf"
FileCopy(logDataFile, strCopyToFolderLdf)
Dim strColl As New System.Collections.Specialized.StringCollection
strColl.Add("dbPatientLabWork.mdf")
strColl.Add("dbPatientLabWork_log.ldf")
srv.AttachDatabase("dbPatientLabWork", strColl) <=== ERROR HERE ======

A runtime error is being generated in the last Line "Attach database failed for Server - Server Name"
Could someone help me sort out the error generated as mentioned above.

What I have tried:

1. The server object srv shows all attached databases when tested.

2. The .mdf and .ldf files of the Database do get copied to the relevant folder DATA of the Sql Server but the database does not get attached.
Posted
Updated 12-Sep-16 9:30am
Comments
RossMW 11-Sep-16 18:41pm    
A couple of options to check.
1. Have you tried having the strColl.Add values with the files full directory path ?
2. Does the script have the appropiate security rights ?
Arvind61 12-Sep-16 12:08pm    
Thanks RossMW. The option 1. provided by you has worked !.I am out of a deadlock in my project.Thank you very much.

1 solution

Just to close the question off..

Set the strColl.Add values with the files full directory path.
 
Share this answer
 

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