如何具体检查SQL数据库是否已打开?-VB.NET 2012 MVC 4

如何具体检查SQL数据库是否已打开?-VB.NET 2012 MVC 4,.net,vb.net,asp.net-mvc-4,.net,Vb.net,Asp.net Mvc 4,我有一个空的SQL结果,没有错误消息 调用上述函数的URL为:http://localhost:1812/Home/SQLSelect 问题是,有没有办法专门检查SQL数据库是否已打开 使用的工具:Visual Studio 2012、VB.NET MVC 4、Microsoft SQL Server Compact 4.0 注意:数据库名称为test\u drive\u database,并且没有为数据库设置密码。您的代码中似乎有一些错误 如果您正在连接到Sql Server Compact,

我有一个空的SQL结果,没有错误消息

调用上述函数的URL为:
http://localhost:1812/Home/SQLSelect

问题是,有没有办法专门检查SQL数据库是否已打开

使用的工具:Visual Studio 2012、VB.NET MVC 4、Microsoft SQL Server Compact 4.0


注意:数据库名称为
test\u drive\u database
,并且没有为数据库设置密码。

您的代码中似乎有一些错误

如果您正在连接到Sql Server Compact,那么连接字符串应该如下所示

    Function SQLSelect() As ActionResult

    Dim theconnection As New SqlConnection("Data Source=(localdb);Database=test_drive_database;")
    Dim queryString As String = "SELECT * FROM ColorTable"
    Dim command As New SqlCommand(queryString)
    command.CommandTimeout = 15
    command.CommandType = CommandType.Text

    'Printing Out the SQL Result

    Return ViewData("command")

End Function

然后,要使用的对象是命名空间中的和

  Data Source=MyData.sdf;Persist Security Info=False;
还有两个问题:您没有打开连接,也没有将连接与命令关联

  System.Data.SqlServerCe 

您的代码中似乎有一些错误

如果您正在连接到Sql Server Compact,那么连接字符串应该如下所示

    Function SQLSelect() As ActionResult

    Dim theconnection As New SqlConnection("Data Source=(localdb);Database=test_drive_database;")
    Dim queryString As String = "SELECT * FROM ColorTable"
    Dim command As New SqlCommand(queryString)
    command.CommandTimeout = 15
    command.CommandType = CommandType.Text

    'Printing Out the SQL Result

    Return ViewData("command")

End Function

然后,要使用的对象是命名空间中的和

  Data Source=MyData.sdf;Persist Security Info=False;
还有两个问题:您没有打开连接,也没有将连接与命令关联

  System.Data.SqlServerCe 

正如Steve所说,您需要调用SQLConnection对象的
Open
方法来打开连接

完成此操作后,可以使用
SQLConnection
对象的
State
属性随时检查连接的状态

  theconnection.Open
  command.Connection = theconnection 

正如Steve所说,您需要调用SQLConnection对象的
Open
方法来打开连接

完成此操作后,可以使用
SQLConnection
对象的
State
属性随时检查连接的状态

  theconnection.Open
  command.Connection = theconnection