是可以在vb.net上执行sql查询吗?

时间:2012-01-30 16:28:22

标签: sql vb.net ado.net

ALTER DATABASE test SET ENABLE_BROKER

这是我希望在程序执行时执行的sql查询。 因为我不想每次改变计算机也需要打开sql管理工具来执行这个查询rath

3 个答案:

答案 0 :(得分:6)

您可以像常规SQL语句一样触发alter database

Dim connection As SqlConnection
connection = New SqlConnection("Data Source=myServerAddress;" & _
    "Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;")
Dim command As SqlCommand
command = connection.CreateCommand()
command.CommandText = "ALTER DATABASE test SET ENABLE_BROKER"
command.ExecuteNonQuery()    

答案 1 :(得分:1)

你可以查看这个链接,虽然它在c#中,你可以通过在线使用任何转换器轻松转换为vb。 Sql scripts using vb.net

答案 2 :(得分:1)

迟到总比不到好。缺少打开和关闭连接。如果有人需要它:

Dim connection As SqlConnection
connection = New SqlConnection("Data Source=myServerAddress;" & _
    "Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;")
connection.Open()
Dim command As SqlCommand
command = connection.CreateCommand()
command.CommandText = "ALTER DATABASE test SET ENABLE_BROKER"
command.ExecuteNonQuery()
connection.Close()