2013-10-28 158 views
1

我是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) 

请帮助。我卡住了。谢谢。

+0

得到它删除使用语句myownSqlCommandBuilderCode并修改我的更新storedproc解决并发问题。 –

回答

0

确定您可以使用现有的存储过程。

使用类似这样的代码(这是C# - 但应该很容易转换为VB。NET):

// create a new SqlCommand as your "insert" command 
da.InsertCommand = new SqlCommand("dbo.YourInsertStoredProcNameHere", con); 

// define it to be a stored procedure 
da.InsertCommand.CommandType = CommandType.StoredProcedure; 

// add any parameters needed... 
da.InsertCommand.Parameters.AddWithValue(....); 

做的UpateCommandDeleteCommand同样的事情。

附注:您应该不是使用sp_前缀为您的存储过程。微软有reserved that prefix for its own use (see Naming Stored Procedures),你将来有可能冒名称冲突的风险。 It's also bad for your stored procedure performance。最好只是简单地避免sp_并使用别的东西作为前缀 - 或根本没有前缀!

+0

Tnx寻求帮助和建议。我将重命名我的storedprocs。我如何将datagridview值传递给da.Parameters? da.insertcommand.parameters.addwithvalue(?) –

+0

我需要循环datagridview吗? –

+0

这是正确的吗? da.insertcommand.parameters.addwithvalue( “@ PARAMNAME”,datagridview.textboxColumn)?我在Savebutton_click事件da.update(dt)上发生错误。连接字符串未启动。 –

相关问题