0
    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

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

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

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

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

注意: 数据库名称为test_drive_database,数据库没有设置密码。

4

2 回答 2

2

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

如果您要连接到 Sql Server Compact,那么连接字符串应该是这样的

  Data Source=MyData.sdf;Persist Security Info=False;

请参阅连接字符串

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

  System.Data.SqlServerCe 

另外两个问题是:您没有打开连接并且没有将连接与命令相关联

  theconnection.Open
  command.Connection = theconnection 
于 2012-11-21T00:03:36.833 回答
1

正如史蒂夫所说,您需要调用OpenSQLConnection 对象的方法来打开连接。

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

    Dim sConn As New SqlConnection(connectionString)
    If sConn.State = ConnectionState.Open Then
        Debug.Print("Your database connection is open")
    ElseIf sConn.State = ConnsectionState.Closed Then
        Debug.Print("Your database connection is closed")
    Else
        Debug.Print("Your database connection state: " & sConn.State.ToString)
    End If
于 2012-11-21T00:34:34.173 回答