新手提问:关于连接数据库

zhaozipeng 2004-07-15 09:53:16
我的原码如下:
Public Class loginform
Inherits System.Windows.Forms.Form
Public user As String
#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 GroupBox1 As System.Windows.Forms.GroupBox
Friend WithEvents username As System.Windows.Forms.TextBox
Friend WithEvents userpass As System.Windows.Forms.TextBox
Friend WithEvents login As System.Windows.Forms.Button
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents Label3 As System.Windows.Forms.Label
Friend WithEvents msg As System.Windows.Forms.Label
Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
Friend WithEvents LinkLabel1 As System.Windows.Forms.LinkLabel
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(loginform))
Me.GroupBox1 = New System.Windows.Forms.GroupBox
Me.LinkLabel1 = New System.Windows.Forms.LinkLabel
Me.msg = New System.Windows.Forms.Label
Me.Label2 = New System.Windows.Forms.Label
Me.Label1 = New System.Windows.Forms.Label
Me.login = New System.Windows.Forms.Button
Me.userpass = New System.Windows.Forms.TextBox
Me.username = New System.Windows.Forms.TextBox
Me.Label3 = New System.Windows.Forms.Label
Me.PictureBox1 = New System.Windows.Forms.PictureBox
Me.GroupBox1.SuspendLayout()
Me.SuspendLayout()
'
'GroupBox1
'
Me.GroupBox1.Controls.Add(Me.LinkLabel1)
Me.GroupBox1.Controls.Add(Me.msg)
Me.GroupBox1.Controls.Add(Me.Label2)
Me.GroupBox1.Controls.Add(Me.Label1)
Me.GroupBox1.Controls.Add(Me.login)
Me.GroupBox1.Controls.Add(Me.userpass)
Me.GroupBox1.Controls.Add(Me.username)
Me.GroupBox1.Location = New System.Drawing.Point(16, 104)
Me.GroupBox1.Name = "GroupBox1"
Me.GroupBox1.Size = New System.Drawing.Size(264, 144)
Me.GroupBox1.TabIndex = 0
Me.GroupBox1.TabStop = False
Me.GroupBox1.Text = "系统登录"
'
'LinkLabel1
'
'
'Label2
'
Me.Label2.ImeMode = System.Windows.Forms.ImeMode.NoControl
Me.Label2.Location = New System.Drawing.Point(40, 56)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(40, 16)
Me.Label2.TabIndex = 4
Me.Label2.Text = "密码:"
'
'Label1
'
Me.Label1.ImeMode = System.Windows.Forms.ImeMode.NoControl
Me.Label1.Location = New System.Drawing.Point(40, 24)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(40, 16)
Me.Label1.TabIndex = 3
Me.Label1.Text = "帐号:"
'
'login
'
Me.login.ImeMode = System.Windows.Forms.ImeMode.NoControl
Me.login.Location = New System.Drawing.Point(80, 88)
Me.login.Name = "login"
Me.login.Size = New System.Drawing.Size(80, 24)
Me.login.TabIndex = 2
Me.login.Text = "登录"
'
'userpass
'
Me.userpass.ImeMode = System.Windows.Forms.ImeMode.NoControl
Me.userpass.Location = New System.Drawing.Point(80, 56)
Me.userpass.Name = "userpass"
Me.userpass.PasswordChar = Microsoft.VisualBasic.ChrW(42)
Me.userpass.Size = New System.Drawing.Size(150, 21)
Me.userpass.TabIndex = 1
Me.userpass.Text = ""
'
'username
'
Me.username.Location = New System.Drawing.Point(80, 24)
Me.username.Name = "username"
Me.username.Size = New System.Drawing.Size(150, 21)
Me.username.TabIndex = 0
Me.username.Text = ""
'
'
'loginform
'
Me.AcceptButton = Me.login
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(292, 285)
Me.Controls.Add(Me.PictureBox1)
Me.Controls.Add(Me.Label3)
Me.Controls.Add(Me.GroupBox1)
Me.MaximizeBox = False
Me.Name = "loginform"
Me.ShowInTaskbar = False
Me.Text = "koko业务流程管理"
Me.GroupBox1.ResumeLayout(False)
Me.ResumeLayout(False)

End Sub

#End Region

Dim myConnection As New SqlConnection
Private Sub loginform_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
myConnection = New SqlConnection("uid=sa;password=19781029;database=test_clase;server=(local)")
End Sub

Private Sub login_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles login.Click
Hide()
Dim mainform As New mainForm
mainform.Show()

End Sub


End Class
出错提示为:
E:\koko管理\koko\login.vb(174): 未定义类型“SqlConnection”。
E:\koko管理\koko\login.vb(177): 未定义类型“SqlConnection”。
请问如何改


...全文
91 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
happyluckyboyMuLin 2004-07-15
  • 打赏
  • 举报
回复
你没有声明数据库连接字符串啊
strXiaoCaiNiao 2004-07-15
  • 打赏
  • 举报
回复
1:引用 Imports ...
2:链接字符串: uid=...
3: ......
zhaozipeng 2004-07-15
  • 打赏
  • 举报
回复
我把 Private Sub login_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles login.Click
Hide()
Dim mainform As New mainForm
mainform.Show()

End Sub
改为: Private Sub login_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles login.Click
If username.Text <> "" Or userpass.Text <> "" Then
Dim InsertCmd As String = "select * from admin where username='" + username.Text + "'"
Dim MyCommand As New SqlCommand(InsertCmd, myConnection)
Dim dr As SqlDataReader
MyCommand.Connection.Open()
dr = MyCommand.ExecuteReader()
End If

End Sub
现在我运行后一执行到MyCommand.Connection.Open()就出现错误:

未处理的“System.Data.SqlClient.SqlException”类型的异常出现在 system.data.dll 中。

其他信息: 系统错误。
zhaozipeng 2004-07-15
  • 打赏
  • 举报
回复
那个错误我解决了,在前面声明一下就可以了,有谁能给我讲下这段代码连接数据库的原理吗?
zhaozipeng 2004-07-15
  • 打赏
  • 举报
回复
谢谢大家,请问如何添加reference to System.Data.dll啊,还有我只在在代码的最开始地方,添加引用Imports System.Data.SqlClient后提示
E:\koko\koko\login.vb(180): 名称“myConnection”未声明。
lilaisi 2004-07-15
  • 打赏
  • 举报
回复
在代码的最开始地方,添加引用Imports System.Data.SqlClient
AntingZ 2004-07-15
  • 打赏
  • 举报
回复
写在第一行

Imports System.Data.SqlClient

Public Class loginform
.....
saucer 2004-07-15
  • 打赏
  • 举报
回复
add a reference to System.Data.dll, and at the top of your page, add

Imports System.Data.SqlClient

16,554

社区成员

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

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