关于数据库查询的问题

angelo_2574 2008-06-10 09:52:26
总是出错,“未处理的OleDbException ,至少一个参数没有被指定值。”怎么也找不出来,麻烦高手!
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Trim(txtID.Text) = "" Then
MsgBox("输入要查询的学生学号!", MsgBoxStyle.Critical, "提示")
Else
myconnection.Open() '打开数据库
Dim mycommandtext As String
mycommandtext = "select * from student where 学号=" & Trim(txtID.Text) 'SQL查询命令
mycommand.CommandText = mycommandtext
mycommand.Connection = myconnection '命令对应的连接对象


Dim myadapter As New OleDbDataAdapter(mycommand)
Dim mydataset As New DataSet
myadapter.Fill(mydataset, "result")
DataGridView1.DataSource = mydataset.Tables("result")
End If
End Sub
...全文
88 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
angelo_2574 2008-06-11
  • 打赏
  • 举报
回复
高手啊!帮忙解决下咯!
angelo_2574 2008-06-10
  • 打赏
  • 举报
回复
没有用啊,还是同样的错误,郁闷!
amandag 2008-06-10
  • 打赏
  • 举报
回复
mycommandtext = "select * from student where 学号=" & Trim(txtID.Text) 'SQL查询命令
==
mycommandtext = "select * from student where 学号='" & Trim(txtID.Text) & "'" 'SQL查询命令
pinyu 2008-06-10
  • 打赏
  • 举报
回复
对了,建议你注释掉myconnection.open。问题多半就得以解决。你解决了问题我告诉你原因,因为我还不太确定
pinyu 2008-06-10
  • 打赏
  • 举报
回复
莫非你输入的学号里有@这个符号?用户输入的学号是什么?
changjiangzhibin 2008-06-10
  • 打赏
  • 举报
回复

'没看到什么错误,提几点建议吧
Public Class Form1

Dim myconnection As New OleDbConnection 'DB连接对象
Dim mycommand As New OleDbCommand '数据库命令


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim connectionstring As String
connectionstring = "provider=microsoft.jet.oledb.4.0; data source=F:\Visual Studio 2008" + _
"\Projects\ziji_通过编程的方式访问ACCESS数据库,并实现查询、插入、删除等操作\" + _
"ziji_通过编程的方式访问ACCESS数据库,并实现查询、插入、删除等操作\bin\student.mdb"
myconnection.ConnectionString = connectionstring
'这种数据库路径是第一次见到,建议改简单些

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Trim(txtID.Text) = "" Then
MsgBox("输入要查询的学生学号!", MsgBoxStyle.Critical, "提示")
Else
myconnection.Open() '打开数据库
Dim mycommandtext As String
mycommandtext = "select * from student where 学号=" & Trim(txtID.Text) 'SQL查询命令
mycommand.CommandText = mycommandtext
mycommand.Connection = myconnection '命令对应的连接对象


Dim myadapter As New OleDbDataAdapter(mycommand)
Dim mydataset As New DataSet
myadapter.Fill(mydataset, "result")
DataGridView1.DataSource = mydataset.Tables("result")
     '上面这一句,一般这样写,如下
       DataGridView1.DataSource = mydataset.Tables[0].DefaultView
DataGridView1.DataBind();
End If
End Sub
angelo_2574 2008-06-10
  • 打赏
  • 举报
回复
myconnection和mycommand是全局对象,数据库的连接对象myconnection已经设置好了,

Public Class Form1

Dim myconnection As New OleDbConnection 'DB连接对象
Dim mycommand As New OleDbCommand '数据库命令


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim connectionstring As String
connectionstring = "provider=microsoft.jet.oledb.4.0; data source=F:\Visual Studio 2008" + _
"\Projects\ziji_通过编程的方式访问ACCESS数据库,并实现查询、插入、删除等操作\" + _
"ziji_通过编程的方式访问ACCESS数据库,并实现查询、插入、删除等操作\bin\student.mdb"
myconnection.ConnectionString = connectionstring


End Sub

16,717

社区成员

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

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