Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql VB.net在连接数据库之前检查数据库是否存在_Sql_Sql Server_Database_Vb.net_Sql Server 2012 - Fatal编程技术网

Sql VB.net在连接数据库之前检查数据库是否存在

Sql VB.net在连接数据库之前检查数据库是否存在,sql,sql-server,database,vb.net,sql-server-2012,Sql,Sql Server,Database,Vb.net,Sql Server 2012,我执行以下查询以确定是否已创建数据库表: if db_id('thedbName') is not null --code mine :) print 'db exists' else print 'nope' 现在我想在我的VB.net应用程序中使用相同的查询。这是我目前在其他地方连接到数据库的代码(在执行所有这些操作之前,我想看看它是否存在): 当然,问题是我必须先知道数据库名称才能连接到数据库 然后,我似乎能够通过以下方式连接到主数据库: Dim cn As SqlCo

我执行以下查询以确定是否已创建数据库表:

if db_id('thedbName') is not null
   --code mine :)
   print 'db exists'
else
   print 'nope'
现在我想在我的VB.net应用程序中使用相同的查询。这是我目前在其他地方连接到数据库的代码(在执行所有这些操作之前,我想看看它是否存在):

当然,问题是我必须先知道数据库名称才能连接到数据库

然后,我似乎能够通过以下方式连接到主数据库:

Dim cn As SqlConnection = New SqlConnection("Data Source=DAVIDSDESKTOP;" & _
                                            "Integrated Security=True;" & _
                                            "Pooling=False")

    Dim sql As String = "if db_id('thedbName') is not null " & vbCrLf & _
                            "Print() 'exists' " & vbCrLf & _
                        "else " & vbCrLf & _
                            "Print() 'nope'"

    Dim cmd As SqlCommand = New SqlCommand(sql, cn)

    cmd.Connection.Open()
    Dim blah As String = cmd.ExecuteNonQuery()
    cmd.Connection.Close()
但该查询似乎在Dim blah上抛出了一个错误,因为String=cmd.ExecuteNonQuery()的:

其他信息:“')”附近的语法不正确

所以我不确定为了纠正查询中的问题,我遗漏了什么

需要知道如何让查询返回并说“存在”或“否”
Print()
更改为
Print
(删除括号)


最好不要使用
打印
,使用
选择

Dim sql As String = "if db_id('thedbName') is not null " & vbCrLf & _
                        "select 'exists' " & vbCrLf & _
                    "else " & vbCrLf & _
                        "select 'nope'"

Dim blah As String = CType(cmd.ExecuteScalar(), string)
ExecuteNonQuery
返回更新和插入受影响的行数。但您正在执行的是一个查询

ExecuteScalar
返回所选第一行的第一列。上面的查询只返回一行和一个值,因此它将返回该行。

Print()
更改为
Print
(删除括号)


最好不要使用
打印
,使用
选择

Dim sql As String = "if db_id('thedbName') is not null " & vbCrLf & _
                        "select 'exists' " & vbCrLf & _
                    "else " & vbCrLf & _
                        "select 'nope'"

Dim blah As String = CType(cmd.ExecuteScalar(), string)
ExecuteNonQuery
返回更新和插入受影响的行数。但您正在执行的是一个查询

ExecuteScalar
返回所选第一行的第一列。上面的查询只返回一行和一个值,因此它将返回这一行。

或者这样做

select * from sys.databases where [name] = 'thedbName'
如果它返回一行,则数据库存在,如果不存在,则数据库不存在

要检查数据库中是否存在表,请使用以下命令

select * from sys.objects where [name] = 'theTableName' and type_desc = 'USER_TABLE'
还是这样做

select * from sys.databases where [name] = 'thedbName'
如果它返回一行,则数据库存在,如果不存在,则数据库不存在

要检查数据库中是否存在表,请使用以下命令

select * from sys.objects where [name] = 'theTableName' and type_desc = 'USER_TABLE'

在将查询包含在代码中之前,您是否首先在数据库上运行了该查询?是的,可能重复使用SQL management studio查询效果很好。@MrGadget不适用,因为我还想返回“exists”或“nope”。在另一篇文章的链接中没有这样的解决方案。应该是
打印存在
没有括号ID您首先在数据库上运行查询,然后再将其包含在代码中?可能重复“是”使用SQL management studio查询效果很好。@MrGadget不适用,因为我还想返回“存在”或“不”。在另一篇文章的链接中,没有这样的解决方案。应该只是
Print exists
,而没有括号来识别它。没有错误,但它不断返回-1,而不是“存在”或“不”伟大的斯科特!哈谢谢你的示例代码。它现在工作得很好!谢谢你发现了。没有错误,但它不断返回-1,而不是“存在”或“不”伟大的斯科特!哈谢谢你的示例代码。它现在工作得很好!