2

私はvb.netが初めてです。我慢してください。にデータを挿入、更新、削除する方法を学びたいDatagridviewです。これまでのところ、これに対する最善のアプローチは?にバインドすることだと学びDatagridViewました。DataTable要件は、ストアド プロシージャを使用することです。データベース テーブルに直接アクセスすることは許可されていません。

私の公開変数:

Public intDisbursementID As Long
Dim CS As String = ConfigurationManager.ConnectionStrings("SimpleAccounting.My.MySettings.SimpleAcctgConnectionString").ConnectionString
Dim cb As SqlCommandBuilder = Nothing
Dim da As SqlDataAdapter = Nothing
Dim ds As DataSet = Nothing
Dim dv As DataView
Dim dt As DataTable
Dim bs As BindingSource
Dim isDataLoaded As Boolean

以下のロードからの私のコード:

Private Sub LoadDetailsData(ByVal mDisbursementID As Long)

    Using con As SqlConnection = New SqlConnection(CS)
        Try
            da = New SqlDataAdapter("sp_NET_tblDisbursementDetails_CompanyID_DisbursementID", CS)
            da.SelectCommand.CommandType = CommandType.StoredProcedure
            da.SelectCommand.Parameters.AddWithValue("@CompanyID", CInt(ConfigurationManager.AppSettings("CompanyID")))
            da.SelectCommand.Parameters.AddWithValue("@DisbursementID", CLng(mDisbursementID))

            cb = New SqlCommandBuilder(da)
            ''======== I have no idea how to make this work ===============
            'da.InsertCommand = SqlCommand.GetInsertCommand()
            'da.UpdateCommand = SqlCommand.GetUpdateCommand()
            'da.DeleteCommand = SqlCommand.GetDeleteCommand()
            '==============================================================

            dt = New DataTable
            bs = New BindingSource
            da.Fill(dt)
            bs.DataSource = dt
            dgvDisbursementDetails.DataSource = bs
            'dgvDisbursementDetails.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader)

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Using

End Sub

私のボタン保存コード:

da.Update(dt)

質問: を機能させる方法がわかりませんSqlCommandBuilderSqlCommandBuilder既存の挿入、更新、削除ストアド プロシージャをオーバーライドして使用する方法はありますか?

私は解決策を見つけたと思います: (しかし、データベース側の副作用については本当に理解していません。) SQLCommandBuilder が挿入、更新、削除コマンドを作成したので、既存の挿入、更新、更新が不要になったということですか?ストアド プロシージャを削除しますか? 私が働いている会社では、テーブルへの直接アクセスは許可されていません。

私の更新されたコードの下:

Private Sub LoadDetailsData(ByVal mDisbursementID As Long)


    Using con As SqlConnection = New SqlConnection(CS)
        Try
            'con.Open()
            da = New SqlDataAdapter("sp_NET_tblDisbursementDetails_CompanyID_DisbursementID", CS)
            da.SelectCommand.CommandType = CommandType.StoredProcedure
            da.SelectCommand.Parameters.AddWithValue("@CompanyID", CInt(ConfigurationManager.AppSettings("CompanyID")))
            da.SelectCommand.Parameters.AddWithValue("@DisbursementID", CLng(mDisbursementID))

            ds = New DataSet
            da.Fill(ds)
            ds.Tables(0).TableName = "Disbursements"

            dgvDisbursementDetails.DataSource = ds.Tables("Disbursements")

        Catch ex As Exception
            Throw ex
            'MessageBox.Show(ex.Message)
        End Try
    End Using

End Sub

cmdSaveDetails_Click コード:

    cb = New SqlCommandBuilder(da)
    da.Update(ds, "Disbursements")

ストアドプロシージャを使用して独自の挿入、更新、削除コマンドを作成することにより、SQLCOMMANDBUILDER を複製しようとしています。buttonSave_Click でこのエラーが発生しました: sqladapter.Update(dtable) ERROR: "同時実行違反: UpdateCommand は、予想される 1 レコードのうち 0 に影響を与えました"

私の更新されたコードの下:

グローバル変数:

Public intDisbursementID as  Long
Dim CS As String = ConfigurationManager.ConnectionStrings("SimpleAccounting.My.MySettings.SimpleAcctgConnectionString").ConnectionString
Dim sqladapter As SqlDataAdapter
Dim dset As DataSet
Dim dtable As DataTable

フォーム ロード コード:

LoadDisbursementDetails(intDisbursementID)
myownSqlCommandBuilder(intDisbursementID)

LoadDisbursementDetails サブコード:

Private Sub LoadDisbursementDetails(ByVal mDisbursementID As Long)

    Using con As SqlConnection = New SqlConnection(CS)

        Try
            sqladapter = New SqlDataAdapter("sproc_DisbursementDetailsSelectByDisbursementID", con)

                sqladapter.SelectCommand.CommandType = CommandType.StoredProcedure
                sqladapter.SelectCommand.Parameters.AddWithValue("@DisbursementID", CLng(mDisbursementID))

                dset = New DataSet
                dtable = New DataTable

                sqladapter.Fill(dset)
                sqladapter.Fill(dtable)

                dgvDisbursementDetails.DataSource = dtable


        Catch ex As Exception
            Throw ex
        End Try

    End Using

End Sub

myownSqlCommandBuilder サブコード:

Private Sub myownSqlCommandBuilderCode(ByVal mDisbursementID As Long)

    Using con As SqlConnection = New SqlConnection(CS)

        Dim delete As New SqlCommand("sproc_DisbursementDetailsDelete", con)
        delete.CommandType = CommandType.StoredProcedure
        delete.Parameters.Add("@DisbursementDetailsID", SqlDbType.BigInt, 8, "DisbursementDetailsID")

        Dim insert As New SqlCommand("sproc_DisbursementDetailsInsert", con)
        insert.CommandType = CommandType.StoredProcedure
        insert.Parameters.Add("@DisbursementID", SqlDbType.BigInt, 8, "DisbursementID")
        insert.Parameters.Add("@CompanyID", SqlDbType.BigInt, 8, "CompanyID")
        insert.Parameters.Add("@DatePosted", SqlDbType.DateTime, 8, "DatePostedID")
        insert.Parameters.Add("@SLID", SqlDbType.BigInt, 8, "SLID")
        insert.Parameters.Add("@Amount", SqlDbType.BigInt, 8, "Amount")
        insert.Parameters.Add("@UserID", SqlDbType.BigInt, 8, "UserID")

        Dim update As New SqlCommand("sproc_DisbursementDetailsUpdate", con)
        update.CommandType = CommandType.StoredProcedure
        update.Parameters.Add("@DisbursementID", SqlDbType.BigInt, 8, "DisbursementID")
        update.Parameters.Add("@CompanyID", SqlDbType.BigInt, 8, "CompanyID")
        update.Parameters.Add("@DatePosted", SqlDbType.DateTime, 8, "DatePostedID")
        update.Parameters.Add("@SLID", SqlDbType.BigInt, 8, "SLID")
        update.Parameters.Add("@Amount", SqlDbType.BigInt, 8, "Amount")
        update.Parameters.Add("@UserID", SqlDbType.BigInt, 8, "UserID")
        update.Parameters.Add("@DisbursementDetailsID", SqlDbType.BigInt, 8, "DisbursementDetailsID")


        '==== Error: object reference not set to an instance of an object ====
        sqladapter.DeleteCommand = delete
        sqladapter.InsertCommand = insert
        sqladapter.UpdateCommand = update
        '======================================================================

        sqladapter.MissingSchemaAction = MissingSchemaAction.AddWithKey

    End Using

End Sub

ボタン保存コード:

 sqladapter.Update(dtable)

助けてください。私は立ち往生しています。ありがとう。

4

2 に答える 2

0

次のコードと注を使用できます。文字列は私のプロシージャ名です

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Try
        Dim str As String = "server=.;UID=sa;PWD=123456;database=MYdatabase"
        Dim con As New SqlConnection(str)
        Dim com As String = "proc_Ticket_Details"
        Dim Adpr As New SqlDataAdapter(com, con)
        Dim ds As New DataSet()
        Adpr.Fill(ds)
        DataGridView1.DataSource = ds.Tables(0)
        'MessageBox.Show("View Data here")

    Catch ex As Exception
        MessageBox.Show("ERROR: " & ex.Message)            
    End Try
End Sub
于 2019-02-12T07:18:46.207 に答える