Thursday, February 9, 2012

Attach SQL DB to SQL Server 2005 using VB.Net

Is there any way to attach a SQL DB to SQL Server 2005 using VB.Net? If so can you also set security?

Can you rephrase your question? Attach SQL DB from which version of SQL server ? Look up books online for sp_attachdb and sp_detachdb.

|||

I have a DB that was created in SQL Server 2005. I have a website that displays data from systems all over the world. Each system sends it's data to it's own DB. The customer can then go to a website and see the data at real time. What I am trying to do is automate the process for new systems. Each system has it's own portion of the website and its own DB. When an employee enters information into a form on the website it creates the new portion of the site and inserts a new DB into SQL Server 2005. I have everything working except for inserting the DB into SQL Server 2005.

|||

I figured it out thanks for pointing me in the right direction with the sp_attach_db. The accual code is:

Dim objConnAsNew SqlConnection("Server=Web-Testbed;uid=CreateDBs;pwd=ABC123;database=master")

objConn.Open()

Dim objCmdAsNew SqlCommand("EXEC sp_attach_db @.dbname = N'" & DBname &"', @.filename1 = N'X:\sites\" & DBname &"\Database\" & DBname &".mdf', @.filename2 = N'X:\sites\" & DBname &"\Database\" & DBname &"_log.ldf'", objConn)

objCmd.CommandType = CommandType.Text

objCmd.ExecuteNonQuery()

objConn.Close()

No comments:

Post a Comment