扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:黎宇 来源:yesky 2007年10月15日
关键字: Visual Basic 开发 MIS
Public Class CDataBase Dim OleCnnDB As New OleDbConnection() '连接Oracle数据库,ServerName:服务器名,UserId:用户名,UserPwd:用户密码 Public Function ConnOracle(ByVal ServerName As String, ByVal UserId As String, ByVal UserPwd As String) As OleDbConnection Dim OleCnnDB As New OleDbConnection() With OleCnnDB .ConnectionString = "Provider=MSDAORA.1;Password='" & UserPwd & "';User ID='" & UserId & "';Data Source='" & ServerName & "'" Try .Open() Catch er As Exception MsgBox(er.ToString) End Try End With mOleCnnDB = OleCnnDB Return OleCnnDB End Function '获取数据集。TableName:表名,strWhere:条件 Public Overloads Function GetDataSet(ByVal TableName As String, ByVal strWhere As String) As DataSet Dim strSql As String Dim myDataSet As New DataSet() Dim myOleDataAdapter As New OleDbDataAdapter() myOleDataAdapter.TableMappings.Add(TableName, TableName) strSql = "SELECT * FROM " & TableName & " where " & strWhere myOleDataAdapter.SelectCommand = New OleDbCommand(strSql, mOleCnnDB) Try myOleDataAdapter.Fill(myDataSet) Catch er As Exception MsgBox(er.ToString) End Try Return myDataSet End Function '获取物理表。TableName:表名 Public Overloads Function GetDataTable(ByVal TableName As String) As DataTable Dim myDataSet As New DataSet() myDataSet = GetDataSet(TableName) Return myDataSet.Tables(0) End Function '获取物理表。TableName:表名,strWhere:条件 Public Overloads Function GetDataTable(ByVal TableName As String, ByVal strWhere As String) As DataTable Dim myDataSet As New DataSet() myDataSet = GetDataSet(TableName, strWhere) Return myDataSet.Tables(0) End Function '向物理表中插入一行数据。TableName:表名,Value:行数据,BeginColumnIndex:开始列 Public Overloads Function Insert(ByVal TableName As String, ByVal Value As Object, Optional ByVal BeginColumnIndex As Int16 = 0) As Boolean Dim myDataAdapter As New OleDbDataAdapter() Dim strSql As String Dim myDataSet As New DataSet() Dim dRow As DataRow Dim i, len As Int16 strSql = "SELECT * FROM " & TableName myDataAdapter.SelectCommand = New OleDbCommand(strSql, mOleCnnDB) Dim custCB As OleDbCommandBuilder = New OleDbCommandBuilder(myDataAdapter) myDataSet.Tables.Add(TableName) myDataAdapter.Fill(myDataSet, TableName) dRow = myDataSet.Tables(TableName).NewRow len = Value.Length For i = BeginColumnIndex To len - 1 If Not (IsDBNull(Value(i)) Or IsNothing(Value(i))) Then dRow.Item(i) = Value(i) End If Next myDataSet.Tables(TableName).Rows.Add(dRow) Try myDataAdapter.Update(myDataSet, TableName) Catch er As Exception MsgBox(er.ToString) Return False End Try myDataSet.Tables.Remove(TableName) Return True End Function '更新物理表的一个字段的值。strSql:查询语句,FieldName_Value:字段及与对应的值 Public Overloads Sub Update(ByVal strSql As String, ByVal FieldName_Value As String) Dim myDataAdapter As New OleDbDataAdapter() Dim myDataSet As New DataSet() Dim dRow As DataRow Dim TableName, FieldName As String Dim Value As Object Dim a() As String a = strSql.Split(" ") TableName = a(3) a = FieldName_Value.Split("=") FieldName = a(0).Trim Value = a(1) myDataAdapter.SelectCommand = New OleDbCommand(strSql, mOleCnnDB) Dim custCB As OleDbCommandBuilder = New OleDbCommandBuilder(myDataAdapter) myDataSet.Tables.Add(TableName) myDataAdapter.Fill(myDataSet, TableName) dRow = myDataSet.Tables(TableName).Rows(0) If Value <> Nothing Then dRow.Item(FieldName) = Value End If Try myDataAdapter.Update(myDataSet, TableName) myDataSet.Tables.Remove(TableName) Catch er As Exception MsgBox(er.ToString) End Try End Sub '删除物理表的数据。TableName:表名,strWhere:条件 Public Overloads Sub Delete(ByVal TableName As String, ByVal strWhere As String) Dim myReader As OleDbDataReader Dim myCommand As New OleDbCommand() Dim strSql As String strSql = "delete FROM " & TableName & " where " & strWhere myCommand.Connection = mOleCnnDB myCommand.CommandText = strSql Try myReader = myCommand.ExecuteReader() myReader.Close() Catch er As Exception MsgBox(er.ToString) End Try End Sub End Class |
Public Class CData:Inherits CDataBase |
'所要操作的表名 Private Shared UpdateTableName As String '所要操作的表对象 Public Shared UpdateDataTable As New DataTable() '对应表的一行数据197 Public Shared ObjFields() As Object '表的字段数 Public Shared FieldCount As Int16 '主关键字。我们假设每个物理表都有一个主关键字字段fSystemID Public Shared SystemID As String |
Public Property UpdateTable() As String Get UpdateTable = UpdateTableName End Get Set(ByVal Value As String) UpdateTableName = Value.Trim UpdateDataTable = DB.GetDataTable(UpdateTableName) UpdateTableFieldNames = UpdateDataTable.Clone FieldCount = UpdateDataTable.Columns.Count ReDim ObjFields(FieldCount - 1) End Set End Property '删除由主关键值fSystemID指定的数据行 Public Sub Delete() Dim strSQL As String strSQL = "Delete from " & UpdateTableName & " where fSystemID=" & SystemID DB.Delete(strSQL) UpdateDataTable.Rows.Remove(GetRow) End Sub '向表UpdateTableName中插入一行数据。数据由ObjFields给出 Public Function Insert() As Boolean DB.Insert(UpdateTableName, ObjFields) End Function '更新表UpdateTableName所指定的行 Public Shadows Sub Update() Dim SetField As String Dim i As Int16 For i = 1 To FieldCount - 1 SetField = UpdateTableFieldNames.Columns(i).ColumnName & "=" & ObjFields(i) UpdateField(SetField) Next End Sub Public Sub UpdateField(ByVal SetField As String) Dim StrSQL As String StrSQL = "select * from " & UpdateTableName & " where fSystemID= " & SystemID DB.Update(StrSQL, SetField) End Sub '填充网络数据 Public Overloads Sub FillGrid(ByVal GridName As DataGrid) GridName.DataSource = UpdateDataTable End Sub '把数据网格的当前行数据定写入到输入控件中 Public Sub DataGridToText(ByVal frm As Form) Dim RowIndex, i As Int16 Dim value Dim obj As Control Dim DataGrid As New DataGrid() If FieldCount = 0 Then Exit Sub For Each obj In frm.Controls If obj.GetType.Name = "DataGrid" Then DataGrid = obj Exit For End If Next RowIndex = DataGrid.CurrentRowIndex For i = 1 To FieldCount - 1 value = DataGrid.Item(RowIndex, i) If IsDBNull(value) = True Then value = "" End If For Each obj In frm.Controls ' If obj.TabIndex = i Then obj.Text = value Exit For End If Next Next End Sub |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者