July 19, 2007 at 11:15 am
Hello,
Question Posted Here: http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1786146&SiteID=1
I am trying to attach and detach a database for SQL 2000 inside of my program without a log file and I am having a few problems with it. Any help would be greatly appreciated.
1) A few notes I only want to use SMO, I do not want to use SQLDMO on the client computers so the code snippet "SQLDMO Attach-Detach" is out.
2) I am using SQL 2000.
3) I want to do this only by C# or VB.Net.
SQLDMO Attach-Detach
' Connect to the MSDE SQL Server
Dim dmoServer As New SQLDMO.SQLServer
dmoServer.Connect(serverName, logIn, password)
' Attach the Database
Dim strResult As String = dmoServer.AttachDBWithSingleFile(attachDatabaseAs, fi.FullName)
If (strResult IsNot Nothing) Then
dmStop(strResult, "SQL Attach Error for " + attachDatabaseAs)
Return False
End If
I am attaching the database with only the database (.MDF) file. For some reason it grabs another databases LDF (Log File) and makes that the log file. (Not sure why? Any answers to that?)
Attach Database SQL SMO
Dim svc As New Microsoft.SqlServer.Management.Common.ServerConnection
With svc
.ApplicationName = Application.ProductName
.ServerInstance = serverName
.StatementTimeout = 30
.LoginSecure = False
.Login = userName
.Password = password
End With
Dim server As New SqlServerSmo.Server(svc)
sc.Add(fi.FullName)
server.AttachDatabase(attachDatabaseAs, sc)
I then tried to delete the logfile and create a new log file. I could not get that to work. It would have two log files with the database.
Delete Log File Create New Log File
If server.Databases(attachDatabaseAs).LogFiles.Count > 0 Then _
server.Databases(attachDatabaseAs).LogFiles.Item(0).MarkForDrop(True)
server.Alter()
Dim logFile As New Microsoft.SqlServer.Management.Smo.LogFile( _
server.Databases(attachDatabaseAs), String.Format("{0}_Log", attachDatabaseAs), _
String.Format("{0}\{1}_Log.LDF", strDirectory, attachDatabaseAs))
logFile.GrowthType = SqlServerSmo.FileGrowthType.Percent
logFile.Growth = 10
logFile.Create()
Pretty much I am looking for the solution I had with SQLDMO but with the SMO instead. (dmoServer.AttachDBWithSingleFile). Does anyone know of any way to do this?
If there is no way to do it through SQL SMO is there anyway to run a script without connecting to a database, just the server?
Again thank you in advance for any help I receive.
If there is anything I did not explain enough please let me know.
Thank you,
Jeff
Viewing post 1 (of 1 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy