- 在线时间
- 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数据库
1 I4 `2 L/ g J) B( @+ B* e * `2 w; d" C9 ?3 g; [+ p
大家好,以下是有关ASP.net连接SQL Server2000数据库的例程, % `3 S* I6 B) l
在这里和大家分享一下: % g+ O3 @3 j/ B0 @
9 L1 Y a' c3 z5 Y
Asp.net连接SQL Server2000数据库例程详解:
7 g# a, q- l r2 r: i/ Z Q( R <%@ Import Namespace="System.Data" %>
4 C- ^* [3 n2 K- h6 v7 b <%@ Import NameSpace="System.Data.SqlClient" %> + d- T' @& |/ M3 n6 G7 ?
<script laguage="VB" runat="server">
3 `1 D: X+ O t sub page_load(sender as Object,e as EventArgs) ; _- |& _8 p" e" |
Dim myConnection As SqlConnection
! O4 L* h% H/ b$ J" T Dim myCommand As SqlCommand
+ g. {: C# }- J- z$ g3 i- V# a Dim ds as DataSet " I- [) x2 d! f; E" J
'1.connect to sql server ' G5 y% t* S* v$ E7 V% {
myConnection = New SqlConnection( "server=localhost;database=Pubs;uid=ueytjdf;pwd=doekdf" )
0 b" S4 f# [$ h% Q3 x y1 D3 k myConnection.Open() 3 @7 S0 u6 B$ i% r4 V# Z
la1.text="Connection Opened!"
: n5 W8 y) J5 J3 m" ?- q8 y& t( N, c 8 E4 g4 d3 `- O, k3 o S( J
'2.Create a table
7 L) ^6 K% m m% E0 ] myCommand = New SqlCommand( "CREATE TABLE [test] ([id] [int] IDENTITY (1, 1) NOT NULL ,[name]
% b( V/ n8 e. J# X' [+ r; ]
. R3 e) K" j- Q [char] (10) COLLATE Chinese_PRC_CI_AS NULL ,[sex] [char] (10) COLLATE Chinese_PRC_CI_AS NULL : `& Q' g5 S! \1 c1 A2 O
2 B4 c. Z( B: g r
)", myConnection ) . B: X& \, M- D! q9 `
myCommand.ExecuteNonQuery()
$ U" J- a( C& G! r. U6 P/ H la2.text="New table created!" . r' x2 d# \1 h, Q4 I
& V2 x2 r5 g6 h7 i4 Y; t '2 添加纪录
. l5 _0 F1 S" U/ o0 M' n myCommand = New SqlCommand( "Insert into [test] (name,sex) values( '黄志文','男' )",
- N: j$ r5 `' j
6 f! T9 h* k$ I1 g8 K myConnection )
+ ?- j- `% f( c, r1 u9 v myCommand.ExecuteNonQuery() : @/ f: D6 r# ~9 Q' O
la3.text="New Record Inserted!"
5 ?9 q( N: V: h5 A( r* Q 1 h) t) F& C2 }# H) X
'3 更新数据
: I: w* C8 H0 W2 Q: y myCommand = New SqlCommand( "UPDATE [test] SET name='Smith' where name='李明'", myConnection )
' _8 m; g" Y5 @4 O; D. h) ^. v myCommand.ExecuteNonQuery() / }) J; q$ q$ [5 ~. `# a2 F0 q
la4.text="Record Updated!" - `8 d. R0 x' K; c
p J8 k5 t9 [* B8 p '4 删除数据
( N. P5 B5 t% Y: L7 O2 y myCommand = New SqlCommand( "delete from [test] where name='Smith'", myConnection ) 2 E6 c& X$ w2 ~2 ] D# ]2 k8 Q C5 Y; A
myCommand.ExecuteNonQuery() # O2 O" A' R6 `$ L' C
la5.text="Record Deleted!" " U' J- N# K5 _0 I
: J, c0 T5 \% c% _$ D '5 用DataGrid显示数据
+ f$ |5 w' N3 u/ X6 Z3 N8 N) l myCommand = New SqlCommand( "select * from [test]", myConnection )
& m2 N6 M, f' s MyDataGrid.DataSource=myCommand.ExecuteReader() 1 u6 j; P( ]) g2 s& I/ p
MyDataGrid.DataBind()
$ [/ X E- J* `0 H/ U end sub
6 @8 w' |4 T' d+ ]$ i. D- N </script>
$ h _+ P. L3 Y. |* Y) [6 |# } <html> 7 _( G1 ], x+ ^6 w) a) s0 m
<body> ; w$ a3 @2 x7 C+ b: N; y" A3 A# J3 r2 |
<asp:label id="la1" runat="server" /><br>
- h% j( Q, H& f <asp:label id="la2" runat="server" /><br>
; Q+ v: c8 _* b& F# H$ m <asp:label id="la3" runat="server" /><br> 5 o. O( M1 s7 b4 ~# W0 d3 I) C
<asp:label id="la4" runat="server" /><br>
, x: J; h5 _0 A4 ~5 B <asp:label id="la5" runat="server" /><br>
8 M! h" N9 v; I( ?6 ]- \5 O1 [ <ASP ataGrid id="MyDataGrid" runat="server" ) M$ U. i( u" c* v$ g
BorderColor="black" # n+ i& h% G8 r* e! ?8 ?
BorderWidth="1"
8 F$ ^- b) j; y" o1 ] GridLines="Both" . ~7 l: L: d$ @- {; Z
CellPadding="3"
1 ?" m# n& \3 c C8 q( h CellSpacing="0" Y1 n6 ]- R x0 k8 `/ V
Font-Name="Verdana"
* m' _- m6 h/ `5 Q7 {" A2 _ Font-Size="10pt" & y% V, L3 N5 R
HeaderStyle-BackColor="#aaaadd"
+ ~; M3 Z4 i4 l) O; ]. b( E AlternatingItemStyle-BackColor="#eeeeee" 2 X2 P5 F# M, |; m
> 3 }' e& X7 a) m7 j& M
</asp ataGrid> $ R5 t7 s; U' Q8 R$ I; X
' `) ~* ?5 Y- ~" H9 J7 {
</body> 3 F; V# r7 N% V7 y! K4 |9 [+ j
</html>
$ M; o3 G) N I- k/ v. t1 f ------------------------------------------------------------------------- + y9 T+ g) o+ {8 }/ j
推荐空间:
4 R. q; p" M$ s% W6 N) ~& n 5 p) U5 P2 g; h5 X, R. ?* I' s
◆商务型C 送CN域名 1600元/年 6 l; b' a# v% M+ N8 w5 Y
独立500网站空间 ,同时支持 ASP, ASP.NET; : q" W b, L, Y* p f1 h. W
提供SQLSERVER2000的数据库存,支持ACCESS数据库 1 \$ |3 ~/ m" A' P
送800MB邮箱邮箱
- I8 E& ^% S. m& |
9 v, h. u- c4 x# ?$ \8 ~% e6 E ◆虚拟专用C型机 送CN域名 4000元/年 4 g) d. ^8 z/ U8 I5 }3 v2 ^9 C
3000M的网站空间,同时支持 ASP, ASP.NET
4 \4 M# r" |/ W+ ]) p* f 支持SQLSERVER2000/access数据库,数据库空间不限 + [; F+ K# H& o {) v3 w
送2000M VIP 企业邮局 ! [' _0 }( O8 D( _0 ?5 H8 s/ [
------------------------------------------------------------------------ 4 |# e: D) ?! r0 w
同时有本月特大的优惠: 8 H/ n, o9 q( H/ \9 G* V
" X' z! w. r# ]/ y, ^# B* T ◆增强C型主机 送CN域名 送300M 邮局 698 元/年
1 }, d1 L5 p/ | l+ a; C) f. t ◆基本C型主机 送.CN或.COM域名 送100Mb邮局 348 元/年
5 R, d9 y) m% L! V- \! g ********************************************************************
/ s7 e2 t$ R1 n ★购买或者续费时代互联的产品,产品7折大优惠!1 T. k8 W0 M* N
详情请见:http://now.net.cn/special/index_chris.net8 z$ B3 o Y* ]6 ~, o
★.CN+地区.CN 仅需180元!7 l$ }. W5 A" }3 ]
.Cn+VDNS无限+.COM+地区.CN 仅需228元!
' r5 R! N3 x0 S z! B( y9 I 更多优惠,详情请见:http://now.net.cn/special/</P>
. p3 v( b6 l; _< > 全球免费咨询电话,请点击 <a href="http://todayisp.com/customer/moreline.net" target="_blank" >http://todayisp.com/customer/moreline.net</A> ; P4 Z* m- D' E* t
Q Q:188092185 342186899
J5 n& X* U% V6 i! _( x TEL:0756-2281071 </P> |
zan
|