<>★Asp.net如何连接SQL Server2000数据库 . M% K3 ^( o0 k9 T2 N . v U3 i: Q# B. I9 D 大家好,以下是有关ASP.net连接SQL Server2000数据库的例程, % S6 h# i- L! T8 A' j7 H 在这里和大家分享一下: 4 N( X& l) C6 ?* c( y, P' S9 W
- r" M: L8 Z# [! n2 E1 _8 E Asp.net连接SQL Server2000数据库例程详解: / s; u8 `. R5 P
<%@ Import Namespace="System.Data" %> 0 ~ Y+ U% {. N; |5 r
<%@ Import NameSpace="System.Data.SqlClient" %> # Y3 F' B) D, |) t# C- r) l8 D
<script laguage="VB" runat="server"> ( s& m) s0 y% V: D sub page_load(sender as Object,e as EventArgs) 6 i E- e6 n: d
Dim myConnection As SqlConnection 5 e, ? b/ o9 }
Dim myCommand As SqlCommand * h% G& u8 E- y8 H& @
Dim ds as DataSet 1 S0 c* i# E* E& t, j- \ '1.connect to sql server t2 K9 o+ Q' Y) x" X2 H
myConnection = New SqlConnection( "server=localhost;database=Pubs;uid=ueytjdf;pwd=doekdf" ) 3 o5 s( c" b0 O' k( J8 I: ]* S2 v myConnection.Open() / o6 h/ y) i& U7 y
la1.text="Connection Opened!" " T/ ~, B( C! |$ d+ y3 y) k5 U7 e 5 ^& \% s [. J# R/ u. m, `
'2.Create a table 4 b' K9 n3 x s' h6 Z/ s" K5 |) L myCommand = New SqlCommand( "CREATE TABLE [test] ([id] [int] IDENTITY (1, 1) NOT NULL ,[name] 5 s2 x S6 h; }& w , B5 X* V6 E4 @1 r" Z; x( V [char] (10) COLLATE Chinese_PRC_CI_AS NULL ,[sex] [char] (10) COLLATE Chinese_PRC_CI_AS NULL ; c6 U' j6 T& x3 y {
' I- @" b( b5 ] )", myConnection ) ! D, \% f1 }' m8 K$ ^* @9 ~
myCommand.ExecuteNonQuery() % ~! A J1 L8 f
la2.text="New table created!" , k% N) x; [' Y- y5 }" v* x. {( _* T# N - m6 q& e" \/ [8 P* j( L' |6 I
'2 添加纪录 ! z$ H5 e2 {9 C9 H, z* W. p! |' _
myCommand = New SqlCommand( "Insert into [test] (name,sex) values( '黄志文','男' )", + N+ l3 a3 o e( f/ M
9 S" o u- f1 m2 w, Y myConnection ) 0 Q- k3 n- m# F" R myCommand.ExecuteNonQuery() 1 h9 N1 ^6 M& k6 N- b
la3.text="New Record Inserted!" 5 N# w5 i$ @. s2 k$ ^$ c9 v
) ^, x2 @# M5 _7 D# K! y1 h '3 更新数据 `2 |( B- X1 b) Z$ P' q/ d% X' S myCommand = New SqlCommand( "UPDATE [test] SET name='Smith' where name='李明'", myConnection ) 2 e, T1 @) ?: y6 I& n
myCommand.ExecuteNonQuery() 6 _% Z9 y: d: d- j0 _( d; `
la4.text="Record Updated!" I8 S7 B" E, ^/ l- `* p 2 _. v: `) j/ k+ G# ~8 \( @ '4 删除数据 % m, D1 Z% E0 `. J! T
myCommand = New SqlCommand( "delete from [test] where name='Smith'", myConnection ) * t" P' K$ S6 d0 J! r! t9 O$ O myCommand.ExecuteNonQuery() / K1 w, _: z0 e. a' Y la5.text="Record Deleted!" - x- C9 w/ n/ P9 a 3 B! L; ^( P# N9 w0 x* n+ n% m" u# o5 n8 x '5 用DataGrid显示数据 ; l) O' c6 z. J$ |" `! J0 a2 A
myCommand = New SqlCommand( "select * from [test]", myConnection ) 0 Z3 Z7 T. O+ j' E5 Q0 g' U2 A MyDataGrid.DataSource=myCommand.ExecuteReader() , p+ n0 F6 n9 x! C$ s
MyDataGrid.DataBind() ; F# l% H8 m% ^: | end sub 4 \; A! ^4 d% ~ </script> # p. s* p2 J7 N* |3 b8 Y! u <html> 2 W; ?$ i, D5 ^* r. ?/ m <body> 7 {* I% f m' Y& b9 `7 E7 \
<asp:label id="la1" runat="server" /><br> 4 \6 J/ f: R( c" k <asp:label id="la2" runat="server" /><br> % }' M) j) P" A1 c0 U
<asp:label id="la3" runat="server" /><br> ( n' N6 C8 T9 Q* T6 V- y
<asp:label id="la4" runat="server" /><br> ) E) [! I$ j8 t, Y. u <asp:label id="la5" runat="server" /><br> " q1 Y3 V5 `7 K! f% S6 X <ASPataGrid id="MyDataGrid" runat="server" , @6 X# k+ n5 c) i BorderColor="black" ; Y+ G/ z, E+ v+ Z; d0 I BorderWidth="1" 6 p5 a% A, R8 ]% k GridLines="Both" 6 F& j+ K+ ^. C+ K S" w2 ~
CellPadding="3" 9 _- c+ |8 X4 s/ \% L3 ` CellSpacing="0" $ Y* D9 ^ o# W5 e; r Font-Name="Verdana" - Y9 @! t+ \8 N R6 q3 N9 d Font-Size="10pt" 8 e! p* ^" d! h" \/ @, Q# _ HeaderStyle-BackColor="#aaaadd" 2 q. ~' C: w. }" g0 e8 i AlternatingItemStyle-BackColor="#eeeeee" 6 Z* s/ {. i$ H9 I, e
> * i, v# @$ a+ o" m9 A5 R </aspataGrid> $ h9 }" l: A8 B R % g- n- L$ t) J4 z) l9 D% e2 o# r# G) i </body> ! k: `( X" O4 s! ~
</html> + } T# X$ }' A$ a ------------------------------------------------------------------------- - q" |) `8 S/ G9 `. i' \9 S1 K* `$ D
推荐空间: X2 d8 \* o& M D) F5 P: K
* G* U* q+ l) X& V4 q5 v7 |! S/ \
◆商务型C 送CN域名 1600元/年 ; u* u$ _- K4 x2 E# V: d1 Z6 k 独立500网站空间 ,同时支持 ASP, ASP.NET; " W0 t. N, ]% d/ v4 w* @, h
提供SQLSERVER2000的数据库存,支持ACCESS数据库 7 S! T7 ?7 w! @$ S: Y: K7 N
送800MB邮箱邮箱 + X2 r }0 H/ ^/ U 4 W- ?* @: z( O ◆虚拟专用C型机 送CN域名 4000元/年 4 {2 U3 \ U* [+ r: {% @" f 3000M的网站空间,同时支持 ASP, ASP.NET ! p& v0 T8 g9 {) G8 b3 U7 h 支持SQLSERVER2000/access数据库,数据库空间不限 + H0 C( t: Y8 j! n 送2000M VIP 企业邮局 4 a# g/ K8 @. B/ a
------------------------------------------------------------------------ . p7 X/ s: F; e* q
同时有本月特大的优惠: $ Q: G5 N# b0 m, a
8 ^2 m- i; P/ ^! t- u4 ?1 R
◆增强C型主机 送CN域名 送300M 邮局 698 元/年 ( n! W* M7 Q' q ◆基本C型主机 送.CN或.COM域名 送100Mb邮局 348 元/年 & b0 g* {) b" _! P0 A6 Y; R ******************************************************************** 6 ? f+ e r0 P, `4 P& g/ e. q ★购买或者续费时代互联的产品,产品7折大优惠!( _/ ^* R* V3 i! L2 T: w( F/ `
详情请见:http://now.net.cn/special/index_chris.net1 O& Q% P* ]4 L& {
★.CN+地区.CN 仅需180元! * h2 x; K# i1 d- q. k! n) @ .Cn+VDNS无限+.COM+地区.CN 仅需228元!- c6 B: ]9 a3 k- s" e1 u3 k8 d
更多优惠,详情请见:http://now.net.cn/special/</P> * B2 l% O' ^4 U( \. u<> 全球免费咨询电话,请点击 <a href="http://todayisp.com/customer/moreline.net" target="_blank" >http://todayisp.com/customer/moreline.net</A> " ]* U+ ]7 R1 c) _) X6 v' R* I$ c Q Q:188092185 342186899 0 W7 M9 {, v$ V$ K6 v2 T. r9 @8 M TEL:0756-2281071 </P>