a simply VB.net/ SQL SERVER question

I

Ina

Hello all,

I am newbien in VB.net and I would like to know what is going wrong
with my code.

Imports System
Imports System.Data
Imports System.Data.SqlClient


Module Module1


Sub Main()
Dim ConnectionString As String
Dim reader As SqlDataReader
Dim sql As String
'connection string; just open a connection with sql server ; Do
I need to mention the
'database here even if I did not create it yet
ConnectionString = "server=localhost;uid=xxxxx;pwd=xxxxxx;"
Console.WriteLine("OK")
Dim sqlConn As New SqlConnection(ConnectionString)
'if the database doesn't exist, create it
sql = "USE MASTER GO IF NOT EXISTS (SELECT * FROM sysdatabases
where name = 'TEST') CREATE DATABASE TEST ON PRIMARY GO"

Dim SelectCommand As New SqlClient.SqlCommand(sql, sqlConn)

Try
sqlConn.Open()

Catch ex As Exception
Console.WriteLine("Failed to execute command")
Finally
sqlConn.Close()
End Try
Console.WriteLine("Done")

End Sub

End Module


Probleme: I have the Ok and Done, and not failed to execute command,
but my database has not been created why?

Ina
 
G

Guest

Ina,

I suspect that you will get better responses to your question in the ADO.Net
or SQL Server news groups.

However, I can point out a few problems:

You need to use one of the command object's Execute methods to actually
execute the SQL statements. For example, you might use

SelectCommand.ExecuteNonQuery()

to actually execute your SQL.

Also, you cannot execute a batch of T-SQL statements via ADO.NET using the
"GO" separator. Instead you would need to execute each command individually.
For example, you would first need to execute "USE MASTER", then execute the
next statement in the batch, etc.

Finally, I think that the "ON PRIMARY" at the end of your CREATE DATABASE
statement is incorrect and will result in a syntax error.

Kerry Moorman
 
S

sirfunusa

If you are new at this, then I would recommend just using an existing
database. In all my years of programming, I've never need to
programmically do a "Create Database". Although I'm sure others have...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top