VB.NET想实现一个功能,将数据库表中的信息显示在textbox中。请大神帮忙看看,指点一下
点击“查询数据”按钮,能够将表中的对应信息显示在texebox中。我写的代码如下:PrivateSubButton1_Click_2(senderAsObject,eAs...
点击“查询数据”按钮,能够将表中的对应信息显示在texebox中。
我写的代码如下:
Private Sub Button1_Click_2(sender As Object, e As EventArgs) Handles Button1.Click
TextBox1.Text = ""
TextBox2.Text = ""
TextBox4.Text = ""
TextBox5.Text = ""
If Not conn Is Nothing Then conn.Close() '判断conn,不为null,关闭conn'
Dim connStr As String '申明变量’
connStr = String.Format("server={0};user id={1}; password={2};database=gao;pooling=false", "localhost", "root", "123456") '登录mysql数据库 为本机,用户名:root 密码:root
Dim sql As String = "select * from xinxin where name='" & TextBox1.Text & "'"
'Dim oledbconn1 As System.Data.OleDb.OleDbConnection = New System.Data.OleDb.OleDbConnection(connStr)
Try
conn = New MySqlConnection(connStr) '实例化连接对象'
conn.Open() '打开连接'
Dim transaction As MySqlTransaction = conn.BeginTransaction '实例化事务对象,开始事务'
mysqlcommand = New MySqlCommand(sql, conn, transaction) '实例化带事务的数据库操作'
mysqlcommand.CommandType = CommandType.Text '操作类型为文本'
mysqlcommand.ExecuteNonQuery() '执行操作,返回受影响的行数'
transaction.Commit() '提交事务,这里才真正开始执行数据库操作'
mysqlcommand = Nothing '清空数据库操作对象'
transaction = Nothing '清空事务对象'
MessageBox.Show("查询成功!")
Catch ex As MySqlException 'try执行过程中发生错误就会提示'
' MessageBox.Show(ex.ToString)
MessageBox.Show("程序出现错误!请重启,或联系维护人员。", "抱歉")
Finally
conn.Close() '最后执行关闭读取连接对象
End Try
End Sub 展开
我写的代码如下:
Private Sub Button1_Click_2(sender As Object, e As EventArgs) Handles Button1.Click
TextBox1.Text = ""
TextBox2.Text = ""
TextBox4.Text = ""
TextBox5.Text = ""
If Not conn Is Nothing Then conn.Close() '判断conn,不为null,关闭conn'
Dim connStr As String '申明变量’
connStr = String.Format("server={0};user id={1}; password={2};database=gao;pooling=false", "localhost", "root", "123456") '登录mysql数据库 为本机,用户名:root 密码:root
Dim sql As String = "select * from xinxin where name='" & TextBox1.Text & "'"
'Dim oledbconn1 As System.Data.OleDb.OleDbConnection = New System.Data.OleDb.OleDbConnection(connStr)
Try
conn = New MySqlConnection(connStr) '实例化连接对象'
conn.Open() '打开连接'
Dim transaction As MySqlTransaction = conn.BeginTransaction '实例化事务对象,开始事务'
mysqlcommand = New MySqlCommand(sql, conn, transaction) '实例化带事务的数据库操作'
mysqlcommand.CommandType = CommandType.Text '操作类型为文本'
mysqlcommand.ExecuteNonQuery() '执行操作,返回受影响的行数'
transaction.Commit() '提交事务,这里才真正开始执行数据库操作'
mysqlcommand = Nothing '清空数据库操作对象'
transaction = Nothing '清空事务对象'
MessageBox.Show("查询成功!")
Catch ex As MySqlException 'try执行过程中发生错误就会提示'
' MessageBox.Show(ex.ToString)
MessageBox.Show("程序出现错误!请重启,或联系维护人员。", "抱歉")
Finally
conn.Close() '最后执行关闭读取连接对象
End Try
End Sub 展开
1个回答
展开全部
又碰到你了。。
在 conn.Open() '打开连接' 之后,把内容读到数据集中,dataset和excel差不多,就可以直接操作数据了
Dim da As OleDbDataAdapter
Dim ds As DataSet
da = New OleDbDataAdapter(sql, conn)
ds = New DataSet
da.Fill(ds, "tbSheet") '填充到ds中,表名是tbSheet
'然后就可以每个单元格调用了
TextBox2.Text = ds.Tables("tbSheet").Rows(0).Item("Name")
推荐个网址http://blog.csdn.net/dzweather/article/details/8558659
在 conn.Open() '打开连接' 之后,把内容读到数据集中,dataset和excel差不多,就可以直接操作数据了
Dim da As OleDbDataAdapter
Dim ds As DataSet
da = New OleDbDataAdapter(sql, conn)
ds = New DataSet
da.Fill(ds, "tbSheet") '填充到ds中,表名是tbSheet
'然后就可以每个单元格调用了
TextBox2.Text = ds.Tables("tbSheet").Rows(0).Item("Name")
推荐个网址http://blog.csdn.net/dzweather/article/details/8558659
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |