返回列表 发帖

在.NET环境中,要正确使用ADO.NET数据库技术

在.NET环境中,要正确使用ADO.NET数据库技术,必须正确掌握以下三项技术
一、.NET数据流阅读器3+1技术
0 前提:导入命名空间   imports System.Data.sqlclint
针对SQL Server数据库的类库联合
                     
imports System.Data.oledb  
针对所有数据库的类库联合
1 定义连接对象并且打开连接  
   
Dim   abc   as  string=”Microsoft.jet.OlEDB.4.0.1;user id=sa;password=123456;data source=ssyyrr.mdb”
   Dim  mycon  as  new  Oledbconnection(abc)
   mycon.Open()
2.定义SQL命令字符串和命令对象   
   dim  ML   as  string=” select *  from  score “
      dim  mycom  as  new  oledbcommand()
mycom.connection=mycon
mycom.commandtext=ML
3.定义数据阅读对象并执行阅读
Dim myreader As New oledbdataReader=mycom.executereader()
实现数据流阅读
dim I as Integer
Response.write(“</Table Border=’1’><Tr Align=’Center’>”)
for I=0 to   myreader.fieldcount-1
   Response.write(“<Td>” & myreader.getName(I) & “</Td>”)
next  
Response.write(“</Tr>”)

while myreader.read()
Response.write(“<Tr>”)
for I=0 to   myreader.fieldcount-1
   Response.write(“<Td>” & myreader.getValue(I) & “</Td>”)
next  
Response.write(“</Tr>”)
End while
Response.write(“</Table>”)
Myreader.close()
Mycon.close


二、.NET数据集3+3技术
0 前提:导入命名空间   imports System.Data.sqlclint 类库联合
                       imports System.Data.oledb
1 定义连接  
   Dim   abc   as  string=” Privider=SQLOLEDB.1; user id=sa;password=;
data source=".";;initial catalog=学生成绩”
   Dim  mycon  as  new  sqlconnection(abc)
2.定义适配器   
   dim  ML   as  string=” select *  from  score “
   dim mycom  as  new sqldataadapter1(ML,  mycon)
3.定义数据集
      Dim mydataset As New DataSet
4 打开连接   
      mycon.Open()
5填充数据集   
        mycom.Fill(mydataset, "score")
6 数据绑定:设置Datagrid1的绑定属性与数据集的联系
        DataGrid1.DataSource = mydataset.Tables("scoue")
注意:连接字符串的设置
一、与SQLserver数据库连接的连接字符串
1.格式
Privider=SQLOLEDB.1;packet size=4096;user id=用户名;password=口令;data source=服务器名;initial catalog=数据库名
2.例子:
Privider=SQLOLEDB.1;user id=sa;password=;data source=".";;initial catalog=db
二、与Access数据库连接的连接字符串
1.格式
Privider=Microsoft.jet.OlEDB.4.0;user id=用户名;password=口令;data source=数据库名
2.例子:
Privider= Microsoft.jet.OlEDB.4.0.1;user id=sa;password=123456;data source=syud.mdb

三、.NET数据命令Command 的3+3技术
0 前提:导入命名空间   imports System.Data.sqlclint
                        imports System.Data.oledb
1 定义连接      
   Dim   abc   as  string=” Privider=SQLOLEDB.1;user id=sa;password=;data source=".";;initial catalog=db”
   Dim mycon  as  new  sqlconnection(abc)
2.定义数据命令   
   dim  ML   as  string=” select *  from “
   dim mycom  as  new sqlcommand(mycom,mycon)
3.定义数据集
      Dim mydataset As New DataSet
4打开连接   
      mycon.Open()
5执行命令   
        或执行返回是否存在命令:  if mycom.ExecuteSclar>0 then
             ………..
或执行返回非查询命令:  mycom.ExecuteNonQuery()
或执行数据流读数据命令:  dim myr as sqldataReader=mycom.Executereader
6 数据绑定:设置Datagrid1的绑定属性与数据集的联系
        DataGrid1.text =add(myr(0))
分享到: QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友

返回列表