首页 > 编程 > .NET > 正文

ado.net数据操作全接触二(query,Parameters)

2024-07-10 13:02:50
字体:
来源:转载
供稿:网友

5.1使用sqldatareader进行数据库查询
1: <%@ import namespace="system.data" %>
2: <%@ import namespace="system.data.sqlclient" %>
3:
4: <%
5: dim myconnection as sqlconnection
6: dim mycommand as sqlcommand
7: dim mydatareader as sqldatareader
8:
9: myconnection = new sqlconnection( "server=localhost;uid=sa;database=pubs" )
10: myconnection.open()
11: mycommand = new sqlcommand( "select * from authors", myconnection )
12: mydatareader = mycommand.executereader()
13: while mydatareader.read()
14:  response.write( mydatareader.item( "au_lname" ) )
15: end while
16: mydatareader.close()
17: myconnection.close()
18: %>
19:
5.2在c#中使用sqldatareader 进行数据库查询
 1: <%@ page language="c#" %>
2: <%@ import namespace="system.data" %>
3: <%@ import namespace="system.data.sqlclient" %>
4:
5: <%
6: sqldatareader mydatareader;
7: sqlconnection myconnection = new http://aspfree.com/chapters/sams/graphics/ccc.gifsqlconnection( "server=localhost;uid=sa;database=pubs" );
8: myconnection.open();
9: sqlcommand mycommand = new sqlcommand( "select * from http://aspfree.com/chapters/sams/graphics/ccc.gifauthors", myconnection );
10: mydatareader = mycommand.executereader();
11: while ( mydatareader.read() )
12: {
13:  response.write( mydatareader[ "au_lname" ].tostring() );
14: }
15: mydatareader.close();
16: myconnection.close();
17: %>
18:
5.3使用oledbdatareader进行数据库查询
1: <%@ import namespace="system.data" %>
2: <%@ import namespace="system.data.oledb" %>
3:
4: <%
5: dim myconnection as oledbconnection
6: dim mycommand as oledbcommand
7: dim mydatareader as oledbdatareader
8:
9: myconnection = new oledbconnection( "provider=microsoft.jet.oledb.4.0;data http://aspfree.com/chapters/sams/graphics/ccc.gifsource=c:/authors.mdb" )
10: myconnection.open()
11: mycommand = new oledbcommand( "select * from authors", myconnection )
12: mydatareader = mycommand.executereader()
13: while mydatareader.read
14:  response.write( mydatareader.item( "author" ) )
15: end while
16: mydatareader.close()
17: myconnection.close
18: %>
19:
5.5使用sql parameters
1: <%@ import namespace="system.data" %> 
2: <%@ import namespace="system.data.sqlclient" %>
3:
4: <%
5: dim myconnection as sqlconnection
6: dim mycommand as sqlcommand
7: dim firstname as string = "robert"
8: dim lastname as string = "johnson"
9:
10: myconnection = new sqlconnection( "server=localhost;uid=sa;pwd=secret;database=mydata" )
11: myconnection.open()
12: mycommand = new sqlcommand( "insert authors ( firstname, lastname ) http://aspfree.com/chapters/sams/graphics/ccc.gifvalues ( @firstname, @lastname )", myconnection )
13:
14: mycommand.parameters.add( new sqlparameter( "@firstname", http://aspfree.com/chapters/sams/graphics/ccc.gifsqldbtype.varchar, 30 ))
15: mycommand.parameters( "@firstname" ).value = firstname
16:
17: mycommand.parameters.add( new sqlparameter( "@lastname", http://aspfree.com/chapters/sams/graphics/ccc.gifsqldbtype.varchar, 30 ))
18: mycommand.parameters( "@lastname" ).value = lastname
19:
20: mycommand.executenonquery()
21: myconnection.close()
22: %>
23: record inserted!
5.6使用sql parameters(access)
1: <%@ import namespace="system.data" %> 
2: <%@ import namespace="system.data.oledb" %>
3:
4: <%
5: dim myconnection as oledbconnection
6: dim mycommand as oledbcommand
7: dim firstname as string = "robert"
8: dim lastname as string = "johnson"
9:
10: myconnection = new oledbconnection( "provider=microsoft.jet.oledb.4.0;http://aspfree.com/chapters/sams/graphics/ccc.gifdata source=c:/author2.mdb" )
11: myconnection.open()
12: mycommand = new oledbcommand( "insert into authors ( firstname, lastname ) http://aspfree.com/chapters/sams/graphics/ccc.gifvalues ( @firstname, @lastname )", myconnection )
13:
14: mycommand.parameters.add( new oledbparameter( "@firstname", http://aspfree.com/chapters/sams/graphics/ccc.gifoledbtype.varchar, 30 ))
15: mycommand.parameters( "@firstname" ).value = firstname
16:
17: mycommand.parameters.add( new oledbparameter( "@lastname", http://aspfree.com/chapters/sams/graphics/ccc.gifoledbtype.varchar, 30 ))
18: mycommand.parameters( "@lastname" ).value = lastname
19:
20: mycommand.executenonquery()
21: myconnection.close()
22: %>
23: record inserted!
24:
 
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表