帮我看一下这段代码为什么出错?(100分)

Joewu3 2005-02-27 02:48:22
Imports System.Data.SqlClient

Public Class frmLand
Inherits System.Windows.Forms.Form

#Region " Windows 窗体设计器生成的代码 "

Public Sub New()
MyBase.New()

'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()

'在 InitializeComponent() 调用之后添加任何初始化

End Sub

'窗体重写 dispose 以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer

'注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents txtUser As System.Windows.Forms.TextBox
Friend WithEvents txtPassword As System.Windows.Forms.TextBox
Friend WithEvents btnLand As System.Windows.Forms.Button
Friend WithEvents btnClose As System.Windows.Forms.Button
Friend WithEvents ToolTip1 As System.Windows.Forms.ToolTip
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container
Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(frmLand))
Me.Label1 = New System.Windows.Forms.Label
Me.Label2 = New System.Windows.Forms.Label
Me.txtUser = New System.Windows.Forms.TextBox
Me.txtPassword = New System.Windows.Forms.TextBox
Me.btnLand = New System.Windows.Forms.Button
Me.btnClose = New System.Windows.Forms.Button
Me.ToolTip1 = New System.Windows.Forms.ToolTip(Me.components)
Me.SuspendLayout()
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(16, 16)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(48, 16)
Me.Label1.TabIndex = 0
Me.Label1.Text = "用户名"
'
'Label2
'
Me.Label2.Location = New System.Drawing.Point(16, 48)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(40, 16)
Me.Label2.TabIndex = 1
Me.Label2.Text = "密码"
'
'txtUser
'
Me.txtUser.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
Me.txtUser.Location = New System.Drawing.Point(72, 8)
Me.txtUser.Name = "txtUser"
Me.txtUser.Size = New System.Drawing.Size(152, 21)
Me.txtUser.TabIndex = 2
Me.txtUser.Text = ""
Me.ToolTip1.SetToolTip(Me.txtUser, "请输入用户名!")
'
'txtPassword
'
Me.txtPassword.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
Me.txtPassword.Location = New System.Drawing.Point(72, 40)
Me.txtPassword.Name = "txtPassword"
Me.txtPassword.PasswordChar = Microsoft.VisualBasic.ChrW(42)
Me.txtPassword.Size = New System.Drawing.Size(152, 21)
Me.txtPassword.TabIndex = 3
Me.txtPassword.Text = ""
Me.ToolTip1.SetToolTip(Me.txtPassword, "请输入该用户所对应的密码,区分大小写!")
'
'btnLand
'
Me.btnLand.FlatStyle = System.Windows.Forms.FlatStyle.Flat
Me.btnLand.Location = New System.Drawing.Point(16, 80)
Me.btnLand.Name = "btnLand"
Me.btnLand.Size = New System.Drawing.Size(56, 24)
Me.btnLand.TabIndex = 4
Me.btnLand.Text = "登陆"
'
'btnClose
'
Me.btnClose.FlatStyle = System.Windows.Forms.FlatStyle.Flat
Me.btnClose.Location = New System.Drawing.Point(168, 80)
Me.btnClose.Name = "btnClose"
Me.btnClose.Size = New System.Drawing.Size(56, 24)
Me.btnClose.TabIndex = 5
Me.btnClose.Text = "退出"
'
'frmLand
'
Me.AcceptButton = Me.btnLand
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(240, 110)
Me.Controls.Add(Me.btnClose)
Me.Controls.Add(Me.btnLand)
Me.Controls.Add(Me.txtPassword)
Me.Controls.Add(Me.txtUser)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.Label1)
Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
Me.MaximizeBox = False
Me.Name = "frmLand"
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text = "登陆"
Me.ResumeLayout(False)

End Sub

#End Region

Dim connStr As String = "server=localhost;uid=stu;pwd=stu;database=StudentInfo"
Dim sqlStr As String = "Select * from purview"
Dim objConn As New SqlConnection
Dim conn As IDbConnection = objConn

Dim id() As Char
Dim password() As Char
Dim purview() As Char
Dim i As Integer = 0 '数据库中的第几个用户
Dim mark As Integer = -1 '标记是否存在该用户

Private Function CreateCommand(ByVal query As String) As IDbCommand
Return New SqlCommand(query, objConn)
End Function

Private Sub Record()
Dim command As IDbCommand = CreateCommand(connStr)
Dim reader As IDataReader = command.ExecuteReader() //这里报错,why?

While reader.Read()
id(i) = reader.GetValue(0)
purview(i) = reader.GetValue(1)
password(i) = reader.GetValue(2)
i = i + 1
End While
reader.Close()
End Sub

Private Sub btnLand_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLand.Click
Dim getID As Char = Me.txtUser.Text
Dim getPassword As Char = Me.txtPassword.Text

conn.ConnectionString = connStr
conn.Open()
Record()
For j As Integer = 0 To i
If getID = id(j) Then
mark = j
Exit For
End If
Next
conn.Close()

If mark = -1 Then
MsgBox("出错:不存在该用户或密码错误!", MsgBoxStyle.Critical, "错误")
Exit Sub
Else
If getPassword = password(mark) Then
Select Case purview(mark)
Case "管理员"
Me.Hide()
aFrmAdmin.Show()
Case "用户"
Me.Hide()
aFrmUser.Show()
End Select
Else
MsgBox("出错:不存在该用户或密码错误!", MsgBoxStyle.Critical, "错误")
Exit Sub
End If
End If

End Sub


Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
Application.Exit() '退出
End Sub
End Class

谢谢
...全文
120 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
yzjd 2005-02-27
  • 打赏
  • 举报
回复
up!头晕了。
smallMage 2005-02-27
  • 打赏
  • 举报
回复
REDIM
xiaohuasz 2005-02-27
  • 打赏
  • 举报
回复
你的数组未指定大小引起的,要redim
Joewu3 2005-02-27
  • 打赏
  • 举报
回复
谢谢 Night_Elf(风暴虫) 的帮忙,你说的问题我已经改正,但是又出现了新的问题,麻烦帮忙再看看

Imports System.Data.SqlClient

Public Class frmLand
Inherits System.Windows.Forms.Form

#Region " Windows 窗体设计器生成的代码 "

Public Sub New()
MyBase.New()

'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()

'在 InitializeComponent() 调用之后添加任何初始化

End Sub

'窗体重写 dispose 以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer

'注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents txtUser As System.Windows.Forms.TextBox
Friend WithEvents txtPassword As System.Windows.Forms.TextBox
Friend WithEvents btnLand As System.Windows.Forms.Button
Friend WithEvents btnClose As System.Windows.Forms.Button
Friend WithEvents ToolTip1 As System.Windows.Forms.ToolTip
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container
Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(frmLand))
Me.Label1 = New System.Windows.Forms.Label
Me.Label2 = New System.Windows.Forms.Label
Me.txtUser = New System.Windows.Forms.TextBox
Me.txtPassword = New System.Windows.Forms.TextBox
Me.btnLand = New System.Windows.Forms.Button
Me.btnClose = New System.Windows.Forms.Button
Me.ToolTip1 = New System.Windows.Forms.ToolTip(Me.components)
Me.SuspendLayout()
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(16, 16)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(48, 16)
Me.Label1.TabIndex = 0
Me.Label1.Text = "用户名"
'
'Label2
'
Me.Label2.Location = New System.Drawing.Point(16, 48)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(40, 16)
Me.Label2.TabIndex = 1
Me.Label2.Text = "密码"
'
'txtUser
'
Me.txtUser.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
Me.txtUser.Location = New System.Drawing.Point(72, 8)
Me.txtUser.Name = "txtUser"
Me.txtUser.Size = New System.Drawing.Size(152, 21)
Me.txtUser.TabIndex = 2
Me.txtUser.Text = ""
Me.ToolTip1.SetToolTip(Me.txtUser, "请输入用户名!")
'
'txtPassword
'
Me.txtPassword.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
Me.txtPassword.Location = New System.Drawing.Point(72, 40)
Me.txtPassword.Name = "txtPassword"
Me.txtPassword.PasswordChar = Microsoft.VisualBasic.ChrW(42)
Me.txtPassword.Size = New System.Drawing.Size(152, 21)
Me.txtPassword.TabIndex = 3
Me.txtPassword.Text = ""
Me.ToolTip1.SetToolTip(Me.txtPassword, "请输入该用户所对应的密码,区分大小写!")
'
'btnLand
'
Me.btnLand.FlatStyle = System.Windows.Forms.FlatStyle.Flat
Me.btnLand.Location = New System.Drawing.Point(16, 80)
Me.btnLand.Name = "btnLand"
Me.btnLand.Size = New System.Drawing.Size(56, 24)
Me.btnLand.TabIndex = 4
Me.btnLand.Text = "登陆"
'
'btnClose
'
Me.btnClose.FlatStyle = System.Windows.Forms.FlatStyle.Flat
Me.btnClose.Location = New System.Drawing.Point(168, 80)
Me.btnClose.Name = "btnClose"
Me.btnClose.Size = New System.Drawing.Size(56, 24)
Me.btnClose.TabIndex = 5
Me.btnClose.Text = "退出"
'
'frmLand
'
Me.AcceptButton = Me.btnLand
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(240, 110)
Me.Controls.Add(Me.btnClose)
Me.Controls.Add(Me.btnLand)
Me.Controls.Add(Me.txtPassword)
Me.Controls.Add(Me.txtUser)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.Label1)
Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
Me.MaximizeBox = False
Me.Name = "frmLand"
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text = "登陆"
Me.ResumeLayout(False)

End Sub

#End Region

Dim connStr As String = "server=localhost;uid=stu;pwd=stu;database=StudentInfo"
Dim sqlStr As String = "Select * from purview"
Dim objConn As New SqlConnection
Dim conn As IDbConnection = objConn

Dim id() As Char
Dim password() As Char
Dim purview() As Char
Dim i As Integer = 0 '数据库中的第几个用户
Dim mark As Integer = -1 '标记是否存在该用户

Private Function CreateCommand(ByVal query As String) As IDbCommand
Return New SqlCommand(query, objConn)
End Function

Private Sub Record()
Dim command As IDbCommand = CreateCommand(sqlStr)
Dim reader As IDataReader = command.ExecuteReader()

While reader.Read()
id(i) = reader.GetValue(0) //未将对象引用设置到对象的实例
purview(i) = reader.GetValue(1)
password(i) = reader.GetValue(2)
i = i + 1
End While
reader.Close()
End Sub

Private Sub btnLand_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLand.Click
Dim getID As Char = Me.txtUser.Text
Dim getPassword As Char = Me.txtPassword.Text

conn.ConnectionString = connStr
conn.Open()
Record()
For j As Integer = 0 To i
If getID = id(j) Then
mark = j
Exit For
End If
Next
conn.Close()

If mark = -1 Then
MsgBox("出错:不存在该用户或密码错误!", MsgBoxStyle.Critical, "错误")
Exit Sub
Else
If getPassword = password(mark) Then
Select Case purview(mark)
Case "管理员"
Me.Hide()
aFrmAdmin.Show()
Case "用户"
Me.Hide()
aFrmUser.Show()
End Select
Else
MsgBox("出错:不存在该用户或密码错误!", MsgBoxStyle.Critical, "错误")
Exit Sub
End If
End If

End Sub


Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
Application.Exit() '退出
End Sub
End Class
hamadou 2005-02-27
  • 打赏
  • 举报
回复
呵呵,看的晕了。
Night_Elf 2005-02-27
  • 打赏
  • 举报
回复
Dim command As IDbCommand = CreateCommand(sqlstr)
Night_Elf 2005-02-27
  • 打赏
  • 举报
回复
给你一个例子:
Public Sub ReadMyData(myConnString As String)
Dim mySelectQuery As String = "SELECT OrderID, Customer FROM Orders"
Dim myConnection As New SqlConnection(myConnString)
Dim myCommand As New SqlCommand(mySelectQuery, myConnection)
myConnection.Open()
Dim myReader As SqlDataReader = myCommand.ExecuteReader()
Try
While myReader.Read()
Console.WriteLine((myReader.GetInt32(0).ToString & ", " & myReader.GetString(1)))
End While
Finally
' always call Close when done reading.
myReader.Close()
' always call Close when done reading.
myConnection.Close()
End Try
End Sub 'ReadMyData
应是像上面这样来使用
wj2929 2005-02-27
  • 打赏
  • 举报
回复
IDbCommand ??
怎么没有见过?

按照你的意思
更改
Private Function CreateCommand(ByVal query As String) As IDbCommand
Return New SqlCommand(query, objConn)
End Function
里面的objConn为conn即可
Night_Elf 2005-02-27
  • 打赏
  • 举报
回复
调用CreateCommand时的参数怎么是connStr呢?应是sqlstr呀!
yizhixiaozhu 2005-02-27
  • 打赏
  • 举报
回复
up
Joewu3 2005-02-27
  • 打赏
  • 举报
回复
to hamadou(闵峰):

我这里不是有:
conn.ConnectionString = connStr
conn.Open()


如果要改,该怎么改啊?
hamadou 2005-02-27
  • 打赏
  • 举报
回复
objConn 没有open() ,其实数据库没有连接上,更没有打开!

16,555

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧