vbnet数据库编程(,VBNET编程宝典

vb.net 中如何使用SQL语句查询数据库中的数据

1、首先打开Visual Studio 2008代码窗口,添加引用。

成都创新互联专注于市中企业网站建设,成都响应式网站建设公司,商城网站建设。市中网站建设公司,为市中等地区提供建站服务。全流程按需求定制设计,专业设计,全程项目跟踪,成都创新互联专业和态度为您提供的服务

2、输入以下代码:Public conn1  As SqlConnection = New SqlConnection 。

3、声明关键字 Public;(因为是全局变量,所以用Public 来声明)。

4、如果SQL 数据库就在本机,则用以下代码连接。

5、如果代码太长,影响可读性,可以用空格加"_"后,回车换行即可。

vb.net+数据库编程问题

使用OleDb将数据库绑定datagridview,然后添加RowHeaderMouseClick事件,在此事件中定义选中某一行后在textbox中显示相应的数据,在添加的按钮中定义打开form2,用sql语句查询数据库得到想要的关键字所关联的数据,显示出来就可以了。这里我给你一部分我写过的代码,基本上和你的要求很像,但是不完全一样,你自己研究研究,改改应该就可以了。

'datagridview绑定数据库

Public Class form1

Private ObjetConnection As OleDbConnection

Private ObjetCommand As OleDbCommand

Private ObjetDataAdapter As OleDbDataAdapter

Private ObjetSet As New DataSet()

' SQL语句

Private strSql As String

Private ObjetDataTable As DataTable

Private ObjetDataRow As DataRow

Private Numeroligne As Integer

'定义路径

Private strConn As String

Private ObjetCommandBuilder As OleDbCommandBuilder

Dim dv As New DataView

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

strConn =

"Provider=Microsoft.ACE.OLEDB.12.0; Data source=" Application.StartupPath "\文件名.accdb" /此处文件路径

strSql =

"Select * 表名 "

ObjetConnection =

New OleDbConnection()

ObjetConnection.ConnectionString = strConn

ObjetConnection.Open()

ObjetCommand =

New OleDbCommand(strSql)

ObjetDataAdapter =

New OleDbDataAdapter(ObjetCommand)

ObjetCommand.Connection() = ObjetConnection

ObjetDataAdapter.Fill(ObjetSet,

"表明")

dv.Table = ObjetSet.Tables(

"表名")

DataGridView1.DataSource = dv

 

ObjetConnection.Close()

End Sub

/此处是RowHeaderMouseclick事件

Private Sub DataGridView1_RowHeaderMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles DataGridView1.RowHeaderMouseClick

此处添加显示表中相应信息

Me.TextBox1.text = DataGridView1.SelectedRows(0).Cells(0).Value

Me.TextBox2.text = DataGridView1.SelectedRows(0).Cells(1).Value

.........

End Sub

vb.net 怎么操作数据库

如果楼主熟悉VB6,可以直接在项目中添加ADODB的Com引用,这样你就可以像VB6那样操作数据库了!

另外

.NET Framework中连接数据库要用到ADO.NET。如果要操作Access数据库,要用到System.Data.OleDb命名空间下的许多类。

比如按楼主所说,“我想在textbox1中显示表一中【一些数据】字段下的第一个内容”:

'首先导入命名空间

Imports System.Data

Imports System.Data.OleDb

'然后在某一个事件处理程序中写:

Dim conn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=数据库.accdb;Jet OLEDB:Database Password=MyDbPassword")

Dim command As New OleDbCommand("Select * From 数据表", conn)

conn.Open() '打开数据库连接

Dim reader As OleDbDataReader = command.ExecuteReader() '执行SQL语句,返回OleDbDataReader 对象

Do While reader.Read() '读取一条数据

textbox1.Text += reader("一些数据") VbCrLf

Loop

reader.Close() '关闭OleDbDataReader

conn.Close() '关闭连接

vb.net的数据库连接

1·绑定数据源来进行连接

2.用代码连接

先到数据库建立一个数据库和相应的表

连接数据库的代码:

Dim str As String = "Data Source=服务器名;Initial Catalog=数据库名;Persist Security Info=True;User ID=;Password="

dim conn As SqlClient.SqlConnection

try

conn = New SqlClient.SqlConnection

conn.ConnectionString = str

conn.Open()

Return True

Catch ex As Exception

MsgBox(ex.ToString)

Return False

End Try

登录代码:Dim str As String = "Data Source=服务器名;Initial Catalog=数据库名;Persist Security Info=True;User ID=;Password="

dim conn As SqlClient.SqlConnection

conn = New SqlClient.SqlConnection

conn.ConnectionString = str

conn.Open()

sqlstr = "Select * From Amd Where AmdName='" TextBox1.Text "' And AmdPwd = '" TextBox2.Text "'"

Dim sqlcmd As SqlClient.SqlCommand = New SqlClient.SqlCommand(sqlstr, conn)

Dim dr As SqlClient.SqlDataReader

dr = sqlcmd.ExecuteReader

If dr.Read = True Then '判断一条记录为真

kf.Show() '显示下个窗体

Me.Hide() ’隐藏当前窗体

Else

MessageBox.Show("输入信息有误!", "提示")

TextBox1.Text = ""

TextBox2.Text = ""

End If

vb.net数据库操作

参考一下下面这段代码就可以了。

Imports System.Data

'引入数据库操作类命名空间

Imports System.Data.OleDb

'引入ADO.NET操作命名空间

Public Class FrmModifystInfo

Inherits System.Windows.Forms.Form

Public ADOcmd As OleDbDataAdapter

Public ds As DataSet = New DataSet()

'建立DataSet对象

Public mytable As Data.DataTable

'建立表单对象

Public myrow As Data.DataRow

'建立数据行对象

Public rownumber As Integer

'定义一个整型变量来存放当前行数

Public SearchSQL As String

Public cmd As OleDbCommandBuilder

'======================================================

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

#End Region

'======================================================

Private Sub FrmModifystInfo_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load

'窗体的载入

TxtSID.Enabled = False

TxtName.Enabled = False

ComboSex.Enabled = False

TxtBornDate.Enabled = False

TxtClassno.Enabled = False

TxtRuDate.Enabled = False

TxtTel.Enabled = False

TxtAddress.Enabled = False

TxtComment.Enabled = False '设置信息为只读

Dim tablename As String = "student_Info "

SearchSQL = "select * from student_Info "

ExecuteSQL(SearchSQL, tablename) '打开数据库

ShowData() '显示记录

End Sub

Private Sub ShowData()

'在窗口中的textbox中显示数据

myrow = mytable.Rows.Item(rownumber)

TxtSID.Text = myrow.Item(0).ToString

TxtName.Text = myrow.Item(1).ToString

ComboSex.Text = myrow.Item(2).ToString

TxtBornDate.Text = Format(myrow.Item(3), "yyyy-MM-dd ")

TxtClassno.Text = myrow.Item(4).ToString

TxtTel.Text = myrow.Item(5).ToString

TxtRuDate.Text = Format(CDate(myrow.Item(6)), "yyyy-MM-dd ")

TxtAddress.Text = myrow.Item(7).ToString

TxtComment.Text = myrow.Item(8).ToString

End Sub

Private Sub BtFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtFirst.Click

'指向第一条数据

rownumber = 0

ShowData()

End Sub

Private Sub BtPrev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtPrev.Click

'指向上一条数据

BtNext.Enabled = True

rownumber = rownumber - 1

If rownumber 0 Then

rownumber = 0 '如果到达记录的首部,行号设为零

BtPrev.Enabled = False

End If

ShowData()

End Sub

Private Sub BtNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtNext.Click

'指向上一条数据

BtPrev.Enabled = True

rownumber = rownumber + 1

If rownumber mytable.Rows.Count - 1 Then

rownumber = mytable.Rows.Count - 1 '判断是否到达最后一条数据

BtNext.Enabled = False

End If

ShowData()

End Sub

Private Sub BtLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtLast.Click

'指向最后一条数据

rownumber = mytable.Rows.Count - 1

ShowData()

End Sub

Private Sub BtDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtDelete.Click

mytable.Rows.Item(rownumber).Delete() '删除记录

If MsgBox( "确定要删除改记录吗? ", MsgBoxStyle.OKCancel + vbExclamation, "警告 ") = MsgBoxResult.OK Then

cmd = New OleDbCommandBuilder(ADOcmd)

'使用自动生成的SQL语句

ADOcmd.Update(ds, "student_Info ")

BtNext.PerformClick()

End If

End Sub

Private Sub BtModify_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtModify.Click

TxtSID.Enabled = False '关键字段只读

TxtName.Enabled = True '可读写

ComboSex.Enabled = True

TxtBornDate.Enabled = True

TxtClassno.Enabled = True

TxtRuDate.Enabled = True

TxtTel.Enabled = True

TxtAddress.Enabled = True

TxtComment.Enabled = True

End Sub

Private Sub BtUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtUpdate.Click

If Not Testtxt(TxtName.Text) Then

MsgBox( "请输入姓名! ", vbOKOnly + vbExclamation, "警告 ")

TxtName.Focus()

Exit Sub

End If

If Not Testtxt(ComboSex.Text) Then

MsgBox( "请选择性别! ", vbOKOnly + vbExclamation, "警告 ")

ComboSex.Focus()

Exit Sub

End If

If Not Testtxt(TxtClassno.Text) Then

MsgBox( "请选择班号! ", vbOKOnly + vbExclamation, "警告 ")

TxtClassno.Focus()

Exit Sub

End If

If Not Testtxt(TxtTel.Text) Then

MsgBox( "请输入联系电话! ", vbOKOnly + vbExclamation, "警告 ")

TxtTel.Focus()

Exit Sub

End If

If Not Testtxt(TxtAddress.Text) Then

MsgBox( "请输入家庭住址! ", vbOKOnly + vbExclamation, "警告 ")

TxtAddress.Focus()

Exit Sub

End If

If Not IsNumeric(Trim(TxtSID.Text)) Then

MsgBox( "请输入数字学号! ", vbOKOnly + vbExclamation, "警告 ")

Exit Sub

TxtSID.Focus()

End If

If Not IsDate(TxtBornDate.Text) Then

MsgBox( "出生时间应输入日期格式(yyyy-mm-dd)! ", vbOKOnly + vbExclamation, "警告 ")

Exit Sub

TxtBornDate.Focus()

End If

If Not IsDate(TxtRuDate.Text) Then

MsgBox( "入校时间应输入日期格式(yyyy-mm-dd)! ", vbOKOnly + vbExclamation, "警告 ")

TxtRuDate.Focus()

Exit Sub

End If

myrow.Item(0) = Trim(TxtSID.Text)

myrow.Item(1) = Trim(TxtName.Text)

myrow.Item(2) = Trim(ComboSex.Text)

myrow.Item(3) = Trim(TxtBornDate.Text)

myrow.Item(4) = Trim(TxtClassno.Text)

myrow.Item(5) = Trim(TxtTel.Text)

myrow.Item(6) = Trim(TxtRuDate.Text)

myrow.Item(7) = Trim(TxtAddress.Text)

myrow.Item(8) = Trim(TxtComment.Text)

mytable.GetChanges()

cmd = New OleDbCommandBuilder(ADOcmd)

'使用自动生成的SQL语句

ADOcmd.Update(ds, "student_Info ")

'对数据库进行更新

MsgBox( "修改学籍信息成功! ", vbOKOnly + vbExclamation, "警告 ")

TxtName.Enabled = False

ComboSex.Enabled = False

TxtBornDate.Enabled = False

TxtClassno.Enabled = False

TxtRuDate.Enabled = False

TxtTel.Enabled = False

TxtAddress.Enabled = False

TxtComment.Enabled = False '重新设置信息为只读

End Sub

Private Sub BtCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtCancel.Click

TxtSID.Enabled = False

TxtName.Enabled = False

ComboSex.Enabled = False

TxtBornDate.Enabled = False

TxtClassno.Enabled = False

TxtRuDate.Enabled = False

TxtTel.Enabled = False

TxtAddress.Enabled = False

TxtComment.Enabled = False

End Sub

Public Function ExecuteSQL(ByVal SQL As String, ByVal table As String)

Try

'建立ADODataSetCommand对象

'数据库查询函数

ADOcmd = New OleDbDataAdapter(SQL, "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\student.mdb ")

'建立ADODataSetCommand对象

ADOcmd.Fill(ds, table) '取得表单

mytable = ds.Tables.Item(0) '取得名为table的表

rownumber = 0 '设置为第一行

myrow = mytable.Rows.Item(rownumber)

'取得第一行数据

Catch

MsgBox(Err.Description)

End Try

End Function

End Class

请采纳。

VB.Net数据库编程的问题

你的这个只写删除datagrid表格里的数据啦...跟本就没有写删除数据库的.也没有更新数据库的.

以下是删除按钮的代码..

If MsgBox("数据删除不可恢复,确认删除数据么?", vbYesNo) = vbYes Then

rec.Delete

rec.MoveNext

rec.Requery

End If

以下是form窗体的datagrid显示数据表的代码:

If rec.State = adStateOpen Then rec.Close

SQL = "select * from jbxx" '定义sql查询语句

rec.Open SQL, con, adOpenStatic, adLockOptimistic '打开记录集

Set dg.DataSource = rec '用datagrid控件显示记录集

以下是模块..

Public con As New Connection '定义数据连接,公共变量

Public coon, coom As String

Sub Main()

ChDir App.Path '转换相对路径

con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=cq.mdb;Persist Security Info=False"

'定义数据库连接

con.CursorLocation = adUseClient '以客户端的方式打开

con.Open ' 打开连接

登陆.Show

End Sub

希望能帮到你...


网站标题:vbnet数据库编程(,VBNET编程宝典
转载源于:http://bzwzjz.com/article/hdcijp.html

其他资讯

Copyright © 2007-2020 广东宝晨空调科技有限公司 All Rights Reserved 粤ICP备2022107769号
友情链接: 高端网站建设 外贸网站建设 网站建设推广 成都网站设计 成都网站设计公司 成都网站建设 成都网站设计制作公司 成都网站建设 网站建设开发 企业网站建设 高端网站设计 成都网站制作 成都定制网站建设 做网站设计 商城网站建设 成都响应式网站建设公司 四川成都网站制作 成都网站制作 重庆网站制作 重庆网站设计 网站建设 宜宾网站设计