- |* a1 U8 o* D8 M T* e( u( G 大家好,以下是有关ASP.net连接SQL Server2000数据库的例程, ! M' e5 h2 D: X) K, F% a 在这里和大家分享一下: 9 g) ?6 M: X+ o1 C3 T - @) p# u4 w& W$ e6 t Asp.net连接SQL Server2000数据库例程详解: % Z5 c' [1 z- }- x1 d
<%@ Import Namespace="System.Data" %> . x1 P# e( E; x <%@ Import NameSpace="System.Data.SqlClient" %> $ j% L2 s/ Y& c- v% n <script laguage="VB" runat="server"> % f' A* G: [' V7 S
sub page_load(sender as Object,e as EventArgs) , ^7 L8 a" g: G1 M& d- k- j$ l Dim myConnection As SqlConnection 2 Y+ Z$ {- ~' y, ~8 C# T Dim myCommand As SqlCommand ) V/ R; G1 y9 u; n
Dim ds as DataSet % i1 f: H. H+ T
'1.connect to sql server ( P# p; b* ^8 u9 j* k myConnection = New SqlConnection( "server=localhost;database=Pubs;uid=ueytjdf;pwd=doekdf" ) 3 A4 n& `4 {( E5 b+ j2 l
myConnection.Open() - q4 C" ~( m* V/ M la1.text="Connection Opened!" ) Z7 w, A! l8 Y: J % Z F" }7 k+ \
'2.Create a table 9 P" D2 O y2 j" G+ N
myCommand = New SqlCommand( "CREATE TABLE [test] ([id] [int] IDENTITY (1, 1) NOT NULL ,[name] 6 e( G- z, A2 S
# E. @' y8 N, }' v [char] (10) COLLATE Chinese_PRC_CI_AS NULL ,[sex] [char] (10) COLLATE Chinese_PRC_CI_AS NULL ) q; S, L5 B1 k
5 O& l0 t- t% i i' |4 r$ N! h )", myConnection ) - f, A O. G v# ^9 s1 s myCommand.ExecuteNonQuery() 3 |- @* f1 V+ i# }4 G# s la2.text="New table created!" 8 e: \9 {, E2 O C6 q: k+ r+ v* a '2 添加纪录 & s. u2 k2 M, I* p \/ B" y' c$ t
myCommand = New SqlCommand( "Insert into [test] (name,sex) values( '黄志文','男' )", $ { j) \" w; w! [9 p
8 ]0 ]9 X9 B3 |0 [ myConnection ) ) G' d- f! Z7 M- t0 K
myCommand.ExecuteNonQuery() & B E: x2 j3 \4 t la3.text="New Record Inserted!" & a i. J& r0 ^4 e9 x
2 \0 p& T( {0 H. s$ N8 t/ M& t '3 更新数据 & g( B* g/ k- N: c: }( A2 O
myCommand = New SqlCommand( "UPDATE [test] SET name='Smith' where name='李明'", myConnection ) 9 A) J- g% a% X" p
myCommand.ExecuteNonQuery() - R) I" a) L$ U6 Y% v la4.text="Record Updated!" 6 n7 A* W- d' H/ F" Y( Q- i ! M9 b+ S8 c8 l8 X '4 删除数据 4 _% W$ O, _3 O' O/ J myCommand = New SqlCommand( "delete from [test] where name='Smith'", myConnection ) * @; [/ I3 r; m3 s: K myCommand.ExecuteNonQuery() ( Z" M- F/ @" w5 N8 \! O# Q3 [
la5.text="Record Deleted!" % s+ S- t1 v0 T' u , u* p/ o. E6 h' y% f4 h- R9 ~1 b
'5 用DataGrid显示数据 8 Q/ x7 u5 c& k- z2 d: g* {
myCommand = New SqlCommand( "select * from [test]", myConnection ) 5 j; j5 `7 q% c7 O2 P! \* @ MyDataGrid.DataSource=myCommand.ExecuteReader() ( t1 }1 m s: m- t9 \) P1 M
MyDataGrid.DataBind() ' ]: {4 e" S3 u, y
end sub 1 _5 L6 T, r" t* }4 @; ^ @5 ]/ T </script> 3 U/ c6 e# P( H% b& h
<html> 9 w) L7 d/ v; v1 Y
<body> 7 [) p4 U# b8 n, p. N <asp:label id="la1" runat="server" /><br> ( e d7 Q* U0 `+ U; f
<asp:label id="la2" runat="server" /><br> + a" x3 X! L9 C3 D6 p <asp:label id="la3" runat="server" /><br> 1 B, Y b* z& K& ?: O% ~
<asp:label id="la4" runat="server" /><br> 9 ^7 {4 L. G0 B2 `3 i B. Y <asp:label id="la5" runat="server" /><br> 1 D/ E: ]- ] x6 ^. v <ASPataGrid id="MyDataGrid" runat="server" + q3 V- ~4 O$ z
BorderColor="black" . j4 x: i1 e6 {% c% h
BorderWidth="1" , Y: f" p7 s n GridLines="Both" 9 k4 u1 |( x! |( Q. _
CellPadding="3" + c1 x0 I, h4 P- h1 ?
CellSpacing="0" ! l" ~1 m) w% Q, D% d/ P1 A- J
Font-Name="Verdana" 7 y$ B' A9 w/ J$ w Font-Size="10pt" 3 ~1 o8 c/ R- Q HeaderStyle-BackColor="#aaaadd" & ~ ]+ S) q) Q* Q* e! A AlternatingItemStyle-BackColor="#eeeeee" $ o$ l/ |% P/ ]0 E
> , r$ l' T6 u& s2 O: H" j, c! {
</aspataGrid> 0 L7 O9 ~5 a, r! J, n7 `
$ D/ ?2 A' ~2 t7 W+ M0 n7 f! G </body> 2 g0 n4 {' j+ F! t9 K
</html> / b- C7 ~: W' G8 j# }, O
------------------------------------------------------------------------- 0 `! H6 }" F* }5 Q. ~) P8 a 推荐空间: $ o; D0 K, J2 Z( _' `4 N& J
/ t3 u! {) Z! b1 w+ T
◆商务型C 送CN域名 1600元/年 ; I: ?1 M7 R D L6 F0 j. F 独立500网站空间 ,同时支持 ASP, ASP.NET; " v/ i3 M2 [- [ 提供SQLSERVER2000的数据库存,支持ACCESS数据库 % ]2 U. h) [8 O# h1 |$ x: t2 z 送800MB邮箱邮箱 9 T! J% N5 I/ r0 q, W3 e5 c2 E( Y' G 3 _! B5 H4 D0 i8 `/ E6 K
◆虚拟专用C型机 送CN域名 4000元/年 . X3 D$ }4 b" ~ 3000M的网站空间,同时支持 ASP, ASP.NET ' a8 D+ Q( D0 E
支持SQLSERVER2000/access数据库,数据库空间不限 7 N o0 k. w, f4 R& Z 送2000M VIP 企业邮局 + ~& e. V+ o% d. r u
------------------------------------------------------------------------ # ~0 A7 j v; g0 Q# ]6 Y/ G 同时有本月特大的优惠: * L7 g9 h7 D2 o( m# L- e$ I, N
$ }: q7 G* |! ^2 M ^# d. Z9 r
◆增强C型主机 送CN域名 送300M 邮局 698 元/年 0 x. ~/ g _- U" d' e2 a
◆基本C型主机 送.CN或.COM域名 送100Mb邮局 348 元/年 % z6 ?* W5 H" Z0 N* g& J ******************************************************************** ( Z# }* v3 g- V8 C8 C
★购买或者续费时代互联的产品,产品7折大优惠!/ k- U! R! G7 l+ Y
详情请见:http://now.net.cn/special/index_chris.net $ q4 g) `# V* f3 a3 Z& Z1 R7 v8 m ★.CN+地区.CN 仅需180元!& Z' J c2 U. ~
.Cn+VDNS无限+.COM+地区.CN 仅需228元!& k! V' {! K0 J$ r& q A
更多优惠,详情请见:http://now.net.cn/special/</P>7 {& h X$ P( }
<> 全球免费咨询电话,请点击 <a href="http://todayisp.com/customer/moreline.net" target="_blank" >http://todayisp.com/customer/moreline.net</A> 4 m% P4 r" Y% }$ ]& K7 I8 h Q Q:188092185 342186899% X3 j8 ]3 K- A6 [7 s8 O
TEL:0756-2281071 </P>