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 

さらに 2 つの問題があります。接続を開いていないことと、接続をコマンドに関連付けていないことです。

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

OpenSteve が言ったように、接続を開くには SQLConnection オブジェクトのメソッドを呼び出す必要があります。

これが完了したら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 に答える