首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > VB Dotnet >

VB.NET 2005中,关于dataGridView和数据集绑定,以及更新数据库的有关问题(可能和.net2003差不多)

2012-02-27 
VB.NET 2005中,关于dataGridView和数据集绑定,以及更新数据库的问题(可能和.net2003差不多)在VB.NET2005中

VB.NET 2005中,关于dataGridView和数据集绑定,以及更新数据库的问题(可能和.net2003差不多)
在VB.NET   2005中,把一个表的数据   显示在dataGridView中,然后对这dataGridView
进行删除,更新,插入的操作,   早作完成之后,点击登陆按钮,把操作后的数据更新的数据库中,,

就是这个问题,困扰了我好几天了,,上司说,要用数据集sqlDataAdApter来解决,
做不好就会被炒鱿鱼,,,,请大家一定要帮忙啊。。。

[解决办法]
我也遇到这样的问题我给你一段程序吧,你自己看看:
Imports System.Data
Imports System.Data.SqlClient
Imports System.ComponentModel
Public Class DataBase
'实现接口IDisposable
Implements IDisposable
'数据库连接对象
Private SqlConn As SqlConnection
Public Shared sConn As String = "Persist Security Info=False;Integrated Security=SSPI;database=eisbook;server=localhost;Connect Timeout=30 "
Public Sub Dispose() Implements System.IDisposable.Dispose
Dispose(True)
GC.SuppressFinalize(True)
End Sub
Protected Sub Dispose(ByVal disposing As Boolean)
If disposing <> True Then
Return
End If

If SqlConn Is Nothing = False Then
SqlConn.Dispose()
SqlConn = Nothing
End If
End Sub
Public Sub Open()
If SqlConn Is Nothing = True Then
'建立数据库连接对象
SqlConn = New SqlConnection(DataBase.sConn)
'打开数据库连接
SqlConn.Open()
End If
End Sub
Public Sub Close()
'如果数据库连接对象不为空则关闭数据库连接
If SqlConn Is Nothing = False Then
SqlConn.Close()
End If
End Sub
Public Function RunSelectSQL(ByVal sSQLString As System.String) As DataView
Me.Open()
Dim SqlDS As DataSet = New DataSet
Dim SqlDA As SqlDataAdapter = New SqlDataAdapter(sSQLString, Me.SqlConn)
SqlDA.Fill(SqlDS)
Return SqlDS.Tables(0).DefaultView
End Function
Public Function RunDelOrInsSQL(ByVal sSQLString As System.String) As DataView
Me.Open()
Dim SqlComm As SqlCommand = New SqlCommand(sSQLString, Me.SqlConn)
SqlComm.ExecuteNonQuery()
End Function
End Class

[解决办法]
好象不是很难吧 注意 sqlDataAdApter 对象的保存就可以了
[解决办法]
Dim Cn As New SqlConnection(SqlHelper.ConnectionString)
Cn.Open()
Dim trans As SqlTransaction = Cn.BeginTransaction
'保存明細
Dim dtDetail As DataTable = CType(Me.GrdDtl.DataSource, DataTable).GetChanges
If Not dtDetail Is Nothing Then
For Each dr As DataRow In dtDetail.Rows
Select Case dr.RowState
Case DataRowState.Added
If IsDBNull(dr( "Part_No ")) Then Exit Select
Dim DetailParaInfo() As Object = {dr( "Detail_ID "), _
ReqSc_ID, _
IIf(IsDBNull(dr( "Seqno ")), 0, dr( "Seqno ")), _
dr( "Order_ID "), _
dr( "Size_ID "), _
dr( "Part_No "), _
IIf(IsDBNull(dr( "ReqSc_LQty ")), 0, dr( "ReqSc_LQty ")), _
IIf(IsDBNull(dr( "ReqSc_RQty ")), 0, dr( "ReqSc_RQty ")), _


dr( "ReqDly_Date "), _
IIf(IsDBNull(dr( "ReqSc_LTotalQty ")), 0, dr( "ReqSc_LTotalQty ")), _
IIf(IsDBNull(dr( "ReqSc_RTotalQty ")), 0, dr( "ReqSc_RTotalQty ")), _
dr( "Qty_Unit "), _
dr( "ReqSc_No "), _
dr( "Remark ") _
}
SqlHelper.ExecuteNonQuery(trans, "UP_iReqScDetTbl_ADD ", DetailParaInfo)
Case DataRowState.Modified
Dim DetailParaInfo() As Object = {dr( "Detail_ID "), _
ReqSc_ID, _
IIf(IsDBNull(dr( "Seqno ")), 0, dr( "Seqno ")), _
dr( "Order_ID "), _
dr( "Size_ID "), _
dr( "Part_No "), _
IIf(IsDBNull(dr( "ReqSc_LQty ")), 0, dr( "ReqSc_LQty ")), _
IIf(IsDBNull(dr( "ReqSc_RQty ")), 0, dr( "ReqSc_RQty ")), _
dr( "ReqDly_Date "), _
IIf(IsDBNull(dr( "ReqSc_LTotalQty ")), 0, dr( "ReqSc_LTotalQty ")), _
IIf(IsDBNull(dr( "ReqSc_RTotalQty ")), 0, dr( "ReqSc_RTotalQty ")), _
dr( "Qty_Unit "), _
dr( "ReqSc_No "), _
dr( "Remark ") _
}
SqlHelper.ExecuteNonQuery(trans, "UP_iReqScDetTbl_Update ", DetailParaInfo)
Case DataRowState.Deleted
SqlHelper.ExecuteNonQuery(trans, "UP_iReqScDetTbl_Delete ", dr( "Detail_ID ", DataRowVersion.Original))

End Select
Next
End If
trans.Commit()

热点排行