VB6到VB.Net - 试图找出数据库读取和更新 [英] VB6 to VB.Net - Trying to figure out database reads and updates

查看:63
本文介绍了VB6到VB.Net - 试图找出数据库读取和更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,之前问过但是没有得到我正在寻找的东西soooooo

你们中的任何一位vb.net专家(有一些vb6经验)都有几分钟时间将这个VB6代码转换为VB.net?看看如何在.net中完成这件事会对我有所帮助。



我有很多需要像下面的代码一样扫描桌面并制作对数据库的修正,用户错误等等。



从我读过的内容来看,你需要一个子/函数来读取所有记录而另一个用于读取所有记录。更新 - 并且每个都需要专门为每个表编码



下面的代码只是一起抛出以获得重点(IE我知道的GUID函数)存在于.net - 其中在vb6中我有一个生成代码的函数)



非常感谢提前!



 Global DB As New ADODB.Connection 
Global RS As New Recordset
Sub Main
OpenDBSQLServer,UserName,Password,数据库,DB
OpenRecordset rsXRef,从XRef中选择*,DB
'---------这是有问题的点
随rsXRef
而不是.EOF '遍历所有记录
如果Len(RTrim(!GUID))< 5然后'如果需要编辑...
!GUID = GUID'更新
结束如果
。更新'并更新记录
.MoveNext'移至下一条记录并重复...
Wend
End with
'---------
End Sub
函数GUID
GUID = SomeRandomGUID
结束函数
函数OpenDB(SQLServer,用户,密码,数据库,dbConnection为ADODB.Connection)As Boolean
dbConnection.ConnectionString =Provider ='SQLOLEDB.1'; User id ='&用户& ';密码='&密码& ';坚持安全信息='真';数据源='& SQLServer& ';初始目录='&数据库& ';连接超时= 2;
dbConnection.Open
OpenDB = True
结束函数
公共函数OpenRecordset(RS As Recordset,SQL As String,Conn As ADODB.Connection)As Boolean
OpenRecordset =真
随RS
On Error Resume Next
如果.State = adStateOpen则
。关闭
结束如果
.CursorType = adOpenDynamic
。 LockType = adLockOptimistic
.CursorLocation = adUseClient
。打开SQL,Conn

结束退出函数
结束函数

解决方案

这是一个简单的转换代码。



您可以使用DataAdapter用表中的记录填充数据集你需要更新。



循环浏览vb代码中的记录并更新数据集中的数据。



然后在循环之后,只需调用数据集的Update方法,更改将发布到数据base。



我希望这就是你在寻找的地方,你将会知道如何转换只是连接到数据库的OpenDB函数。

Ok, asked before but didn't get exactly what i was looking for soooooo
Any of you vb.net experts (with some vb6 experience) got a few minutes to convert this VB6 code to VB.net? It would help me a lot to see how this is done in .net

I have a lot of need for something like the code below that will scan a table and make corrections to the database, user error, whatever.

From what i've read, you'd need one sub/function to read all the records and another for the update - and each of these would need to be coded specifically for each table

The code below was just tossed together to get the point across (IE the GUID function I know exists in .net - where in vb6 i had a function that generated the code)

Many thanks in advance!

Global DB As New ADODB.Connection
Global RS As New Recordset
Sub Main
    OpenDB "SQLServer", "UserName", "Password", "Database", DB
    OpenRecordset rsXRef, "Select * from XRef", DB
    ' --------- This is the point in question
    With rsXRef
        While Not .EOF                        ' Loop through all the records
            If Len(RTrim(!GUID)) < 5 Then  ' If an edit is required...
                !GUID = GUID                  ' Make the update
            End If
            .Update                           ' and update the record
            .MoveNext                         ' move to the next record and repeat...
        Wend
    End With
    ' --------- 
End Sub
Function GUID
    GUID = "SomeRandomGUID"
End Function
Function OpenDB(SQLServer, User, Password, Database, dbConnection As ADODB.Connection) As Boolean
    dbConnection.ConnectionString = "Provider='SQLOLEDB.1' ;User id='" & User & "' ;password='" & Password & "' ;Persist Security info='True' ; Data Source='" & SQLServer & "'; Initial Catalog='" & Database & "'; Connect Timeout=2;"
    dbConnection.Open
    OpenDB = True
End Function
Public Function OpenRecordset(RS As Recordset, SQL As String, Conn As ADODB.Connection) As Boolean
    OpenRecordset = True
    With RS 
        On Error Resume Next
        If .State = adStateOpen Then
            .Close
        End If
        .CursorType = adOpenDynamic
        .LockType = adLockOptimistic
        .CursorLocation = adUseClient
        .Open SQL, Conn
    End With
    Exit Function
End Function

解决方案

This is simple code for conversion.

You can use a DataAdapter to fill the dataset with records from the table that you need to update.

Loop through the records like done in the vb code and update the data in the dataset.

Then after the loop, just call the Update method of Dataset and the changes will be posted to database.

I hope this is what you where looking for and that you will be knowing how to convert the OpenDB function coz that is simply connecting to database.


这篇关于VB6到VB.Net - 试图找出数据库读取和更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆