-1

如何将此代码更改为 VB.NET?所以我想用 aDataSet代替 theADODB.Recordset和 anOleDb.OleDbConnection代替ADODB.Connection.

Set oRs = New ADODB.Recordset
  oRs.Open ("Select * from Login Where Username= '" & txtUsername.Text & "'"),oCn,      adOpenStatic, adLockOptimistic, _
 adCmdText

If txtPassword.Text <> oRs.Fields("Password") Then
Call MsgBox("Incorrect Password", vbOKOnly, "Login Error")
txtPassword.Text = ""
txtPassword.SetFocus
Exit Sub
Else

strUserName = txtUsername.Text 'May need in the future project
 strName = oRs.Fields("FirstName") & " " & oRs.Fields("LastName")
 frmInstruction.Show

这是我到目前为止所尝试的:

Dim cmd As OleDbCommand = New OleDbCommand("SELECT * FROM Login WHERE  Username= '" & txtUsername.Text & "' AND password = '" & txtPassword.Text & "' ", oCn)

Dim sdr As OleDbDataReader = cmd.ExecuteReader()

If (sdr.Read() = True) Then

strUserName = txtUsername.Text

frmInstruction.Show()

'but am having issue with this line of code: 
strName = oRs.Fields("FirstName") & " " & oRs.Fields("LastName")
4

1 回答 1

0

'但是这行代码有问题: strName = oRs.Fields("FirstName") & " " & oRs.Fields("LastName")

您可以使用OledbDataReader.GetString读取 FirstName 和 LastName 字段:

Dim firstName = sdr.GetString(sdr.GetOrdinal("FirstName"))
Dim lastName = sdr.GetString(sdr.GetOrdinal("LastName")) 
strName = firstName & " " & lastName 
于 2012-07-11T08:50:12.783 回答