- 在线时间
- 0 小时
- 最后登录
- 2010-9-2
- 注册时间
- 2004-12-23
- 听众数
- 2
- 收听数
- 0
- 能力
- 0 分
- 体力
- 66 点
- 威望
- 0 点
- 阅读权限
- 20
- 积分
- 36
- 相册
- 0
- 日志
- 0
- 记录
- 0
- 帖子
- 33
- 主题
- 1
- 精华
- 0
- 分享
- 0
- 好友
- 0
升级   32.63% 该用户从未签到
 |
< >★Asp.net如何连接SQL Server2000数据库
: P; p6 Y3 B; I+ b+ V / T3 t) @& L* I
大家好,以下是有关ASP.net连接SQL Server2000数据库的例程, ; q* N3 w0 j0 P
在这里和大家分享一下: & v3 [( Q$ W* @2 u
2 |+ F4 u! f; ~- M# p Asp.net连接SQL Server2000数据库例程详解:
! D5 X- P. y- c- ` s* p( e <%@ Import Namespace="System.Data" %>
9 G4 W0 \* d, p <%@ Import NameSpace="System.Data.SqlClient" %> ( e1 E' k8 Z3 X& ~
<script laguage="VB" runat="server"> 1 X J& i$ A3 k ^; t$ _4 N8 G
sub page_load(sender as Object,e as EventArgs)
- I6 i, }# s1 c: o6 f/ y! I Dim myConnection As SqlConnection
+ L8 i5 h' W% }6 }% `1 S Dim myCommand As SqlCommand
% B/ e, t) O, v) R* @ Dim ds as DataSet
+ c& `6 o2 i- \3 A) g0 I '1.connect to sql server ; e* f+ \9 o8 C$ z
myConnection = New SqlConnection( "server=localhost;database=Pubs;uid=ueytjdf;pwd=doekdf" )
1 [$ w6 _) t4 i# o) m, \ myConnection.Open() ; e& m7 O9 D% G
la1.text="Connection Opened!" ; y8 E; g7 f! X
7 Z1 M' S2 ?! n% d( g/ [9 R
'2.Create a table
r8 a" W# u8 m( a( m& o" Z myCommand = New SqlCommand( "CREATE TABLE [test] ([id] [int] IDENTITY (1, 1) NOT NULL ,[name] - Z# t6 a/ y, v# G, L* N- _
) g1 T3 `/ v# V/ m: \+ E4 }
[char] (10) COLLATE Chinese_PRC_CI_AS NULL ,[sex] [char] (10) COLLATE Chinese_PRC_CI_AS NULL
& ^1 d* k% y& Z( s, ~. J
" C- }9 S2 n" W7 o9 Z( `' G )", myConnection )
V+ n, b8 @! ~7 ] myCommand.ExecuteNonQuery()
J7 Z3 B: P+ N; }" t la2.text="New table created!"
. R2 \* ^, k: p4 H . H) H: h' |% h% B
'2 添加纪录 2 `) |) K( x/ z/ e
myCommand = New SqlCommand( "Insert into [test] (name,sex) values( '黄志文','男' )", 4 W6 }, g+ Z3 F* z/ t
* j0 p$ N0 Z) y. ] myConnection ) & |3 ~$ v! F/ Y% R) q$ x& }& ~# E
myCommand.ExecuteNonQuery() ' S" ]; t( }* Y' \' w
la3.text="New Record Inserted!"
1 `9 z) t/ y' T4 U2 d
6 z& L) z% g7 C '3 更新数据 ; f( j4 V7 Z" i& T% t, t/ D
myCommand = New SqlCommand( "UPDATE [test] SET name='Smith' where name='李明'", myConnection )
7 F3 A+ _8 Y9 n/ {+ Q6 P, C6 k myCommand.ExecuteNonQuery() ' p' O" j8 ?# b4 N4 Y
la4.text="Record Updated!" ' L* L% T5 J; b# @" L
% P2 S ]5 ~& L: b9 A
'4 删除数据
+ j0 ?+ f9 I& v myCommand = New SqlCommand( "delete from [test] where name='Smith'", myConnection ) 5 q5 ^6 y2 ?5 d7 a. \4 C
myCommand.ExecuteNonQuery()
" F$ I( _6 E7 V$ [ la5.text="Record Deleted!"
3 \; I) r$ H* }! y % q9 W4 R0 F5 g, a! `
'5 用DataGrid显示数据
+ B! s0 H1 F: Z; P G/ z; d myCommand = New SqlCommand( "select * from [test]", myConnection ) ; E$ Y( v- X# r" Q8 e
MyDataGrid.DataSource=myCommand.ExecuteReader() - K+ w+ W6 A; T! }
MyDataGrid.DataBind()
* F& Z: o# B+ ~. Z1 H+ J9 @7 i end sub & w) ]6 S P8 Q; s
</script>
: o# u. p2 m* z& x! L2 g <html>
, a% S+ g" {$ p6 {( `- l9 h <body>
. S5 T; P7 J- b- E" J. Z <asp:label id="la1" runat="server" /><br> 9 F9 E. z% B8 @# G$ X- A$ E
<asp:label id="la2" runat="server" /><br> / i$ \ v) d! z
<asp:label id="la3" runat="server" /><br> 2 i) I$ r- ?, E6 y$ C- g
<asp:label id="la4" runat="server" /><br>
5 F5 r7 X& J$ ^2 N" p <asp:label id="la5" runat="server" /><br>
$ I" j) F" U3 V/ c4 Y. {( t, ^ <ASP ataGrid id="MyDataGrid" runat="server"
* _& t6 S2 S' L$ x/ K: B BorderColor="black" 5 u c) ~+ v* G! U, O
BorderWidth="1"
' t: r9 C+ ]3 o GridLines="Both"
/ F& Y* r# a" k0 O0 N CellPadding="3" : o' P4 a) K9 v: M; n& `
CellSpacing="0"
6 f& a, A9 \ { Font-Name="Verdana" 7 w- M, q# ]5 n2 Q4 N6 ~" P
Font-Size="10pt"
( d0 @ P+ t% L$ p% w, @5 I HeaderStyle-BackColor="#aaaadd" 4 p( L! u: T i- { \( D
AlternatingItemStyle-BackColor="#eeeeee"
, U* g0 B7 ]0 q( I" ]5 X/ o >
3 |, h4 Z5 N1 x) ~6 \ </asp ataGrid>
# V' f+ G) x$ m) g; E
5 a9 o$ S2 V6 S3 o </body>
: n' M' g- q9 r+ a0 X </html> 5 ~2 ^7 M, j6 p0 U9 ]/ q0 m
-------------------------------------------------------------------------
4 X0 `. W% U- q' }, g) q 推荐空间: n6 b/ N# d7 R1 B5 o
4 j$ L; @/ L! p" o5 w8 r% l x6 q ◆商务型C 送CN域名 1600元/年
" w# g- X/ ?3 O$ H1 a* z 独立500网站空间 ,同时支持 ASP, ASP.NET; & Y. F$ `, O; ~1 ^5 t( \8 S' @
提供SQLSERVER2000的数据库存,支持ACCESS数据库
. [* w" c/ Z0 _% G* F 送800MB邮箱邮箱 . J& }1 {/ G' Z m5 n
2 _6 _- F* I0 J) i! S1 q H4 {8 V* i ◆虚拟专用C型机 送CN域名 4000元/年 # ?3 I; G- o" I1 z4 Q5 A) h) T9 U& B
3000M的网站空间,同时支持 ASP, ASP.NET
- ^7 T5 B. `: Z# |* m9 }+ N 支持SQLSERVER2000/access数据库,数据库空间不限
/ w- N, _( m) l( [ 送2000M VIP 企业邮局 $ i) i, V E1 w; g6 Z& ?3 H5 _0 Z/ R
------------------------------------------------------------------------ & Q- i8 N( D4 ~: K0 R) i
同时有本月特大的优惠: / V' O. Y5 E$ ]
# B' B7 L: I8 i2 s. Y ◆增强C型主机 送CN域名 送300M 邮局 698 元/年 - |) j% V0 d2 h! c/ ^" J, \
◆基本C型主机 送.CN或.COM域名 送100Mb邮局 348 元/年
$ _# f# W0 s- c2 T1 b1 q9 H ********************************************************************
+ [" \$ W* l; A, G1 x/ `9 ~7 O4 R ★购买或者续费时代互联的产品,产品7折大优惠!$ J5 l0 g$ s$ U' w( x* m! q- H
详情请见:http://now.net.cn/special/index_chris.net
0 S0 K+ N$ Q5 n' L' f ★.CN+地区.CN 仅需180元!) B6 p+ F' |( X' H) J0 Q& ^
.Cn+VDNS无限+.COM+地区.CN 仅需228元!) O1 N: H& M2 ?4 _ y- m. m9 m( [
更多优惠,详情请见:http://now.net.cn/special/</P>4 U) ], h0 _3 ~3 A
< > 全球免费咨询电话,请点击 <a href="http://todayisp.com/customer/moreline.net" target="_blank" >http://todayisp.com/customer/moreline.net</A>
4 F4 s, @- n! E0 Y8 T8 |' s Q Q:188092185 342186899
7 p1 }4 _# |7 K! G TEL:0756-2281071 </P> |
zan
|