4

フォーム ロード イベントで、SQL Server データベースに接続します。

Private Sub AddBook_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            myConnection = New SqlConnection("server=.\SQLEXPRESS;uid=sa;pwd=123;database=CIEDC")
            myConnection.Open()

End Sub

この Insert イベントでは、次のコードを使用します。

Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAdd.Click
            Try
                myConnection.Open()
                myCommand = New SqlCommand("INSERT INTO tblBook(BookCode, BookTitle, Author, PublishingYear, Price, EnterDate, CatID, RackID, Amount) VALUES('" & txtBookCode.Text & "','" & txtTitle.Text & "','" & txtAuthor.Text & "','" & txtPublishYear.Text & "','" & txtPrice.Text & "', #" & txtEnterDate.Text & "#, " & txtCategory.Text & "," & txtRack.Text & "," & txtAmount.Text & ")")
                myCommand.ExecuteNonQuery()
                MsgBox("The book named '" & txtTitle.Text & "' has been inseted successfully")
                ClearBox()
            Catch ex As Exception
                MsgBox(ex.Message())
            End Try
            myConnection.Close()
End Sub

そして、次のエラーが発生します。

ExecuteNonQuery: Connection property has not been initialized
4

5 に答える 5

8
  1. 接続割り当て- SQLCommand の接続プロパティを設定していません。コード行を追加せずにこれを行うことができます。これがエラーの原因です。

    myCommand = New SqlCommand("INSERT INTO tblBook(BookCode, BookTitle, Author, PublishingYear, Price, EnterDate, CatID, RackID, Amount) VALUES('" & txtBookCode.Text & "','" & txtTitle.Text & "','" & txtAuthor.Text & "','" & txtPublishYear.Text & "','" & txtPrice.Text & "', #" & txtEnterDate.Text & "#, " & txtCategory.Text & "," & txtRack.Text & "," & txtAmount.Text & ")", MyConnection)
    
  2. 接続処理- Load Handler から「MyConnection.Open」も削除する必要があります。現在行っているように、クリックハンドラーで開いて閉じるだけです。これはエラーの原因ではありません。

  3. パラメーター化された SQL - ストアド プロシージャを使用していないにもかかわらず、SQL パラメーターを利用する必要があります。これはエラーの原因ではありませんコンラッドが思い出したように、元のコードはユーザーから直接 SQL ステートメントに値をダンプします。SQL パラメータを使用しない限り、悪意のあるユーザーがデータを盗みます。

    Dim CMD As New SqlCommand("Select * from MyTable where BookID = @BookID")
    CMD.Parameters.Add("@BookID", SqlDbType.Int).Value = CInt(TXT_BookdID.Text)
    
于 2011-06-15T15:24:35.283 に答える
5

Connectionコマンドでプロパティを設定する必要があります。

myCommand.Connection = myConnection
于 2011-06-15T15:24:34.147 に答える
4

エラー メッセージが意味することのほとんどは、SqlCommand オブジェクトの Connection プロパティが、開いた接続に割り当てられていないことです (この場合は、それを呼び出しましたmyConnection)。

また、ここで一言アドバイス。SQL パラメータの読み取りを行います。サニティ チェックを行わずにユーザー入力から SQL 連結を実行すると、SQL インジェクション攻撃が発生します。

これはそれを行う1つの方法です:

Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAdd.Click
    Try
        myConnection.Open()
        myCommand = New SqlCommand( _
        "INSERT INTO tblBook(BookCode, BookTitle, Author, PublishingYear, Price, " & _
        "                    EnterDate, CatID, RackID, Amount) " & _
        "VALUES(@bookCode, @bookTitle, @author, @publishingYear, @price, @enterDate, " & _
        "       @catId, @rackId, @amount)")
        myCommand.Connection = myConnection
        with myCommand.Parameters
            .AddWithValue("bookCode", txtBookCode.Text)
            .AddWithValue("bookTitle", txtTitle.Text)
            .AddWithValue("author", txtAuthor.Text)
            .AddWithValue("publishingYear", txtPublishYear.Text)
            .AddWithValue("price", txtPrice.Text)
            .AddWithValue("enterDate", txtEnterDate.Text)
            .AddWithValue("catId", txtCategory.Text)
            .AddWithValue("rackId", txtRack.Text)
            .AddWithValue("amount", txtAmount.Text)
        end with
        myCommand.ExecuteNonQuery()
        MsgBox("The book named '" & txtTitle.Text & "' has been inseted successfully")
        ClearBox()
    Catch ex As Exception
        MsgBox(ex.Message())
    End Try
    myConnection.Close()
End Sub
于 2011-06-15T15:44:24.900 に答える
0

接続の使用 (開くだけを含む) を USING ブロック内にラップするようにしてください。接続文字列に web.config を使用すると仮定します。

    Dim connection As New SqlConnection(ConfigurationManager.ConnectionStrings("web.config_connectionstring").ConnectionString)
    Dim query As New String = "select * from Table1"
    Dim command as New SqlCommand(query, connection)

Using connection
   connection.Open()
   command.ExecuteNonQuery()
End Using

そして、ユーザーが入力したものは何でもパラメータ化してください..お願いします!

于 2014-08-26T21:45:10.473 に答える