我是vb.net的新用戶。請多多包涵。我想了解如何在Datagridview
中插入,更新和刪除數據。到目前爲止,我瞭解到最好的方法是將DatagridView
綁定到DataTable
?要求是使用存儲過程。我不允許直接訪問數據庫表。插入更新使用SQL Server存儲過程刪除Datagridview?
我的公共變量:
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)
問:我無法想出一個如何使SqlCommandBuilder
工作。有沒有辦法讓我重寫SqlCommandBuilder
並使用我現有的插入,更新,刪除存儲過程?
我想我找到了一個解決方案:(但我真的不明白數據庫端的副作用。)由於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,刪除使用storedprocedures命令。我卡上buttonSave_Click此錯誤:sqladapter.Update(dtable)錯誤:「併發衝突:在更新命令影響的預期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)
請幫助。我卡住了。謝謝。
得到它刪除使用語句myownSqlCommandBuilderCode並修改我的更新storedproc解決併發問題。 –