Home All Groups Group Topic Archive Search About

Problem with SQL Server Script...

Author
26 Feb 2005 1:54 AM
Raphaël Désalbres
Hello,

I'm having the following problem.

Using the following script works OK when run from Query Analyzer

use master
go
Drop database db_Accounting
go
RESTORE DATABASE db_Accounting FROM DISK='D:\mydb.bak'

But, my question is, how can I make it work through VB.NET code?

Thanks,

Raphaël Désalbres
Author
26 Feb 2005 4:42 PM
Michael Levy
You're probably getting tripped up on the GOs. GO is a batch delimiter that
is only understood by the SQL Server tools (QA, OSQL, etc). You'll have to
split your script at the GOs to form seperate calls so the server.

-Mike


Show quoteHide quote
"Raphaël Désalbres" <raph***@desalbres.com> wrote in message
news:%23AJd256GFHA.2356@TK2MSFTNGP12.phx.gbl...
> Hello,
>
> I'm having the following problem.
>
> Using the following script works OK when run from Query Analyzer
>
> use master
> go
> Drop database db_Accounting
> go
> RESTORE DATABASE db_Accounting FROM DISK='D:\mydb.bak'
>
> But, my question is, how can I make it work through VB.NET code?
>
> Thanks,
>
> Raphaël Désalbres
>
>
>
Are all your drivers up to date? click for free checkup

Author
26 Feb 2005 3:59 PM
Raphaël Désalbres
I tried without the "GO", but it still doesn't work...

like this:
Me.cnAccountingProgram.Close()
Me.cnAccountingProgram.Dispose()
Dim cnMaster As New SqlConnection("Initial Catalog=Master; Data
Source=(local); Integrated Security=SSPI")
Dim cmd1 As New SqlCommand
cnMaster.Open()
cmd1.Connection = cnMaster
cmd1.CommandText = "DROP DATABASE DB_Accounting"
cmd1.ExecuteNonQuery()
cmd1.CommandText = "RESTORE DATABASE DB_Accounting FROM DISK='D:\mydb.bak'"
cmd1.ExecuteNonQuery()
cnMaster.Close()

But I still get an error...

Show quoteHide quote
"Michael Levy" <levy.mich***@gamail.com> escreveu na mensagem
news:O0%23G4ICHFHA.3092@tk2msftngp13.phx.gbl...
> You're probably getting tripped up on the GOs. GO is a batch delimiter
> that is only understood by the SQL Server tools (QA, OSQL, etc). You'll
> have to split your script at the GOs to form seperate calls so the server.
>
> -Mike
>
>
> "Raphaël Désalbres" <raph***@desalbres.com> wrote in message
> news:%23AJd256GFHA.2356@TK2MSFTNGP12.phx.gbl...
>> Hello,
>>
>> I'm having the following problem.
>>
>> Using the following script works OK when run from Query Analyzer
>>
>> use master
>> go
>> Drop database db_Accounting
>> go
>> RESTORE DATABASE db_Accounting FROM DISK='D:\mydb.bak'
>>
>> But, my question is, how can I make it work through VB.NET code?
>>
>> Thanks,
>>
>> Raphaël Désalbres
>>
>>
>>
>
>
Author
26 Feb 2005 6:39 PM
William (Bill) Vaughn
You need to read his answer.
Execute each of the sections (delimited with GO) individually--but without
the GO batch marker.
What errors are you getting?

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________

Show quoteHide quote
"Raphaël Désalbres" <raph***@desalbres.com> wrote in message
news:uQ4xHSCHFHA.3968@TK2MSFTNGP14.phx.gbl...
>I tried without the "GO", but it still doesn't work...
>
> like this:
> Me.cnAccountingProgram.Close()
> Me.cnAccountingProgram.Dispose()
> Dim cnMaster As New SqlConnection("Initial Catalog=Master; Data
> Source=(local); Integrated Security=SSPI")
> Dim cmd1 As New SqlCommand
> cnMaster.Open()
> cmd1.Connection = cnMaster
> cmd1.CommandText = "DROP DATABASE DB_Accounting"
> cmd1.ExecuteNonQuery()
> cmd1.CommandText = "RESTORE DATABASE DB_Accounting FROM
> DISK='D:\mydb.bak'"
> cmd1.ExecuteNonQuery()
> cnMaster.Close()
>
> But I still get an error...
>
> "Michael Levy" <levy.mich***@gamail.com> escreveu na mensagem
> news:O0%23G4ICHFHA.3092@tk2msftngp13.phx.gbl...
>> You're probably getting tripped up on the GOs. GO is a batch delimiter
>> that is only understood by the SQL Server tools (QA, OSQL, etc). You'll
>> have to split your script at the GOs to form seperate calls so the
>> server.
>>
>> -Mike
>>
>>
>> "Raphaël Désalbres" <raph***@desalbres.com> wrote in message
>> news:%23AJd256GFHA.2356@TK2MSFTNGP12.phx.gbl...
>>> Hello,
>>>
>>> I'm having the following problem.
>>>
>>> Using the following script works OK when run from Query Analyzer
>>>
>>> use master
>>> go
>>> Drop database db_Accounting
>>> go
>>> RESTORE DATABASE db_Accounting FROM DISK='D:\mydb.bak'
>>>
>>> But, my question is, how can I make it work through VB.NET code?
>>>
>>> Thanks,
>>>
>>> Raphaël Désalbres
>>>
>>>
>>>
>>
>>
>
>
Author
27 Feb 2005 4:03 PM
Raphaël Désalbres
Hello,

I think I wasn't clear in my question: I need to drop a database and restore
the backup in code (VB.NET).

How can I do?

Thanks,

Raphaël......

Show quoteHide quote
"William (Bill) Vaughn" <billvaRemoveT***@nwlink.com> escreveu na mensagem
news:%23pGdCKDHFHA.3088@tk2msftngp13.phx.gbl...
> You need to read his answer.
> Execute each of the sections (delimited with GO) individually--but without
> the GO batch marker.
> What errors are you getting?
>
> --
> ____________________________________
> William (Bill) Vaughn
> Author, Mentor, Consultant
> Microsoft MVP
> www.betav.com/blog/billva
> www.betav.com
> Please reply only to the newsgroup so that others can benefit.
> This posting is provided "AS IS" with no warranties, and confers no
> rights.
> __________________________________
>
> "Raphaël Désalbres" <raph***@desalbres.com> wrote in message
> news:uQ4xHSCHFHA.3968@TK2MSFTNGP14.phx.gbl...
>>I tried without the "GO", but it still doesn't work...
>>
>> like this:
>> Me.cnAccountingProgram.Close()
>> Me.cnAccountingProgram.Dispose()
>> Dim cnMaster As New SqlConnection("Initial Catalog=Master; Data
>> Source=(local); Integrated Security=SSPI")
>> Dim cmd1 As New SqlCommand
>> cnMaster.Open()
>> cmd1.Connection = cnMaster
>> cmd1.CommandText = "DROP DATABASE DB_Accounting"
>> cmd1.ExecuteNonQuery()
>> cmd1.CommandText = "RESTORE DATABASE DB_Accounting FROM
>> DISK='D:\mydb.bak'"
>> cmd1.ExecuteNonQuery()
>> cnMaster.Close()
>>
>> But I still get an error...
>>
>> "Michael Levy" <levy.mich***@gamail.com> escreveu na mensagem
>> news:O0%23G4ICHFHA.3092@tk2msftngp13.phx.gbl...
>>> You're probably getting tripped up on the GOs. GO is a batch delimiter
>>> that is only understood by the SQL Server tools (QA, OSQL, etc). You'll
>>> have to split your script at the GOs to form seperate calls so the
>>> server.
>>>
>>> -Mike
>>>
>>>
>>> "Raphaël Désalbres" <raph***@desalbres.com> wrote in message
>>> news:%23AJd256GFHA.2356@TK2MSFTNGP12.phx.gbl...
>>>> Hello,
>>>>
>>>> I'm having the following problem.
>>>>
>>>> Using the following script works OK when run from Query Analyzer
>>>>
>>>> use master
>>>> go
>>>> Drop database db_Accounting
>>>> go
>>>> RESTORE DATABASE db_Accounting FROM DISK='D:\mydb.bak'
>>>>
>>>> But, my question is, how can I make it work through VB.NET code?
>>>>
>>>> Thanks,
>>>>
>>>> Raphaël Désalbres
>>>>
>>>>
>>>>
>>>
>>>
>>
>>
>
>
Author
28 Feb 2005 2:34 AM
William (Bill) Vaughn
Ok, I'm paying attention now.
First, you don't have to (should not) drop the database before you restore
it.
However, to do so you need to have sufficient rights--the SA account has
those rights. See BOL for details.
So, to restore all you need is (assuming your connection string includes
"Database=master;")...

RESTORE DATABASE db_Accounting FROM DISK='D:\mydb.bak'

This also assumes there are no other connections that have the target
database open. In some versions you need to start the server in single-user
mode.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________

Show quoteHide quote
"Raphaël Désalbres" <raph***@desalbres.com> wrote in message
news:%23ilRN5OHFHA.3332@TK2MSFTNGP15.phx.gbl...
> Hello,
>
> I think I wasn't clear in my question: I need to drop a database and
> restore the backup in code (VB.NET).
>
> How can I do?
>
> Thanks,
>
> Raphaël......
>
> "William (Bill) Vaughn" <billvaRemoveT***@nwlink.com> escreveu na mensagem
> news:%23pGdCKDHFHA.3088@tk2msftngp13.phx.gbl...
>> You need to read his answer.
>> Execute each of the sections (delimited with GO) individually--but
>> without the GO batch marker.
>> What errors are you getting?
>>
>> --
>> ____________________________________
>> William (Bill) Vaughn
>> Author, Mentor, Consultant
>> Microsoft MVP
>> www.betav.com/blog/billva
>> www.betav.com
>> Please reply only to the newsgroup so that others can benefit.
>> This posting is provided "AS IS" with no warranties, and confers no
>> rights.
>> __________________________________
>>
>> "Raphaël Désalbres" <raph***@desalbres.com> wrote in message
>> news:uQ4xHSCHFHA.3968@TK2MSFTNGP14.phx.gbl...
>>>I tried without the "GO", but it still doesn't work...
>>>
>>> like this:
>>> Me.cnAccountingProgram.Close()
>>> Me.cnAccountingProgram.Dispose()
>>> Dim cnMaster As New SqlConnection("Initial Catalog=Master; Data
>>> Source=(local); Integrated Security=SSPI")
>>> Dim cmd1 As New SqlCommand
>>> cnMaster.Open()
>>> cmd1.Connection = cnMaster
>>> cmd1.CommandText = "DROP DATABASE DB_Accounting"
>>> cmd1.ExecuteNonQuery()
>>> cmd1.CommandText = "RESTORE DATABASE DB_Accounting FROM
>>> DISK='D:\mydb.bak'"
>>> cmd1.ExecuteNonQuery()
>>> cnMaster.Close()
>>>
>>> But I still get an error...
>>>
>>> "Michael Levy" <levy.mich***@gamail.com> escreveu na mensagem
>>> news:O0%23G4ICHFHA.3092@tk2msftngp13.phx.gbl...
>>>> You're probably getting tripped up on the GOs. GO is a batch delimiter
>>>> that is only understood by the SQL Server tools (QA, OSQL, etc). You'll
>>>> have to split your script at the GOs to form seperate calls so the
>>>> server.
>>>>
>>>> -Mike
>>>>
>>>>
>>>> "Raphaël Désalbres" <raph***@desalbres.com> wrote in message
>>>> news:%23AJd256GFHA.2356@TK2MSFTNGP12.phx.gbl...
>>>>> Hello,
>>>>>
>>>>> I'm having the following problem.
>>>>>
>>>>> Using the following script works OK when run from Query Analyzer
>>>>>
>>>>> use master
>>>>> go
>>>>> Drop database db_Accounting
>>>>> go
>>>>> RESTORE DATABASE db_Accounting FROM DISK='D:\mydb.bak'
>>>>>
>>>>> But, my question is, how can I make it work through VB.NET code?
>>>>>
>>>>> Thanks,
>>>>>
>>>>> Raphaël Désalbres
>>>>>
>>>>>
>>>>>
>>>>
>>>>
>>>
>>>
>>
>>
>
>

Bookmark and Share