.net访问数据库的三个基本步骤:
一.连接数据库:
要使用sqlclient命名空间中的sqlconnection类;
定义一个sqlconnection对象: Dim sqlCon As New SqlConnection
要进行的设置有:
1.sqlCon .ConnectionString="server=服务器ip;database=数据库名称;integrated security=ture/false(是否有登录账号密码);uid=用户名
(默认:sa);pwd:登录密码" '设置连接属性
2.sqlCon .open '打开数据库
二.编辑操作命令:
要使用sqlclient命名空间中的sqlcommand类;
定义一个sqlcommand对象: Dim sqlCM As New SqlCommand
要进行的设置有:
1.sqlCM .CommandType = CommandType.Text '指明字符串命令属于什么类型(存储过程名、表明、sql语句)
2.sqlCM .CommandText = "select * from table'" '查询语句
三.读取数据(数据读取有两种操作方式:DataSet和DataReader)
方法一.DataSet(把数据读出,缓存在内存的表中,可以用dataset填充一张datatable,再将表与datagridview绑定,即可方便观察数据,要与SqlDataAdapter
对象合作)
步骤:
Dim MyDataSet As New DataSet '定义一个dataset对象
Dim sqlAd As New SqlDataAdapter '定义一个sqldataadapter对象
设置sqldataadapter属性:
sqlAd.SelectCommand = sqlCM '适配器命令属性配置
sqlAd.SelectCommand.Connection = sqlCnn '适配器连接属性配置
sqlAd.Fill(MyDataSet, "stu") '按照表stu的格式将数据填充到MyDataSet中
至此,数据库中返回的数据已经在表MyDataSet中了,为了方便观察,添加一个DataGridView空间,并与表MyDataSet绑定,用来显示数据,
步骤如下:
DataGridView1.DataMember = "table1" '控件名
DataGridView1.DataSource = MyDataSet.Table(0) '数据源绑定
现在,空间DataGridView1上已经将数据库查询结果(表格)显示出来了。
注: 关于SqlDataAdapter和DataSet 的关系,新手(本人)可能有点蒙,提供几个参考:
http://zhidao.baidu.com/question/165308688.html
http://www.cnblogs.com/caijun520/archive/2013/06/02/3114425.html
http://www.cnblogs.com/eagle1986/archive/2010/01/24/1655327.html
方法二.sqlDataReader(每次读取一行结果,并且读取时连接不能断开,速度快,适用于只读不操作)
先说下思路:由于sqlDataReader对象每次只能读一行数据,我们要显示所有数据,可以先将每行数据逐次添加到一张表中(通过一个元素个数与表
的列数相同的数组),再将该表与DataGridView 控件绑定即可。
步骤:
Dim myread As SqlClient.SqlDataReader = Nothing '定义一个SqlDataReader 对象并赋初值Nothing(否则会警告)
Dim mytable As New DataTable() ’定义一张表,用来存查询到的结果
Dim mystr(3) As String '定义中间数组,我这里表格为4列
'添加列
Dim sno As DataColumn = New DataColumn("sno")
Dim sname As DataColumn = New DataColumn("sname")
Dim ssex As DataColumn = New DataColumn("ssex")
Dim sphone As DataColumn = New DataColumn("sphone")
'将列添加到表格中
mytable.Columns.Add(sno)
mytable.Columns.Add(sname)
mytable.Columns.Add(ssex)
mytable.Columns.Add(sphone)
myread = mycommand.ExecuteReader '执行读取动作
While (myread.Read) '不为空则一直读
myread.GetValues(mystr) '执行读取动作,将一行数据存入mystr数组中
mytable.Rows.Add(mystr) '将数组数据作为一行添加到表中
End While
'将表格与现实控件绑定
DataGridView1.DataSource = mytable
现在,控件DataGridView1上已经将数据库查询结果(表格)显示出来了。
注:SqlDataReader参考:
http://www.cnblogs.com/caijun520/archive/2013/06/02/3114422.html
http://blog.163.com/wzhxxt@126/blog/static/418935632013214370722/
下面分别贴出两种方式的完整代码(你得先拥有两个按钮和一个DataGridView控件):
1 Imports System.Data.Sql 2 Imports System.Data.SqlClient 3 4 5 Public Class Form1 6 7 8 9 'DataReader 10 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 11 12 Dim myconnect As New SqlClient.SqlConnection 13 Dim mycommand As New SqlClient.SqlCommand 14 Dim myread As SqlClient.SqlDataReader = Nothing 15 Dim mytable As New DataTable() 16 Dim mystr(3) As String 17 18 '连接属性 19 myconnect.ConnectionString = "server=192.168.0.100;database=temp;integrated security=true;uid=sa;pwd= " 20 myconnect.Open() '打开数据库 21 22 '设置命令属性 23 mycommand.Connection = myconnect 24 mycommand.CommandType = CommandType.Text 25 mycommand.CommandText = "select * from stu" 26 27 '添加列 28 Dim sno As DataColumn = New DataColumn("sno") 29 Dim sname As DataColumn = New DataColumn("sname") 30 Dim ssex As DataColumn = New DataColumn("ssex") 31 Dim sphone As DataColumn = New DataColumn("sphone") 32 '将列添加到表格中 33 mytable.Columns.Add(sno) 34 mytable.Columns.Add(sname) 35 mytable.Columns.Add(ssex) 36 mytable.Columns.Add(sphone) 37 38 myread = mycommand.ExecuteReader '执行读取动作 39 40 While (myread.Read) '不为空则一直读 41 42 myread.GetValues(mystr) '执行读取动作,将一行数据存入mystr数组中 43 mytable.Rows.Add(mystr) '将数组数据作为一行添加到表中 44 End While 45 46 '将表格与现实控件绑定 47 DataGridView1.DataMember = "mytable" 48 DataGridView1.DataSource = mytable 49 50 myread.Close() '关闭读 51 myconnect.Close() '关闭连接 52 53 End Sub 54 55 56 57 'DataSet 58 Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click 59 60 Dim myconnect As New SqlClient.SqlConnection 61 Dim mycommand As New SqlClient.SqlCommand 62 Dim myadp As New SqlClient.SqlDataAdapter 63 Dim mydataset As New DataSet 64 65 myconnect.ConnectionString = "server=192.168.0.100;database=temp;integrated security=true;uid=sa;pwd= " 66 myconnect.Open() 67 68 mycommand.Connection = myconnect 69 mycommand.CommandType = CommandType.Text 70 mycommand.CommandText = "select * from stu" 71 72 myadp.SelectCommand = mycommand 73 myadp.Fill(mydataset) 74 75 DataGridView1.DataSource = mydataset.Tables(0) 76 77 myconnect.Close() 78 79 End Sub 80 End Class