数学建模社区-数学中国

标题: ★Asp.net如何连接SQL Server2000数据库 [打印本页]

作者: rewd    时间: 2004-12-23 11:19
标题: ★Asp.net如何连接SQL Server2000数据库
<>★Asp.net如何连接SQL Server2000数据库5 t& v' W& y  b" c9 o
     
. q/ q0 `* h, n2 l  Q7 [+ p5 T  }     大家好,以下是有关ASP.net连接SQL Server2000数据库的例程,
8 r+ L& B+ P* U7 N     在这里和大家分享一下: / ]' \; V, D% g
     
" ?2 K/ w3 k8 F& R5 M- Y* f     Asp.net连接SQL Server2000数据库例程详解:
" I+ G5 E2 g2 r# A9 e. A9 ~     &lt;%@ Import Namespace="System.Data" %&gt; ; c+ X, o, Z9 p6 Q1 c  h1 |1 V9 N
     &lt;%@ Import NameSpace="System.Data.SqlClient" %&gt; ' M& k- Q5 h0 \
     &lt;script laguage="VB" runat="server"&gt;
7 s# _" F! d) ~, }     sub page_load(sender as Object,e as EventArgs) 5 x9 x) H- G9 ~% H# K4 ?9 f
     Dim myConnection As SqlConnection
- S0 ^! j8 @8 J, E( }' w! k     Dim myCommand As SqlCommand ! y. i& E$ V% u& g" k9 y1 ]4 M# E
     Dim ds as DataSet   e# Q1 ^5 Y, Z- b7 d" R6 Z
     '1.connect to sql server / ~$ J5 {& F6 w& v( @* ~5 \
     myConnection = New SqlConnection( "server=localhost;database=Pubs;uid=ueytjdf;pwd=doekdf" )
) a8 |7 {! a# P. Z1 y. l     myConnection.Open()
6 A9 T; N# R: |+ w4 {     la1.text="Connection Opened!"
7 I8 Z$ E$ v# m3 c8 o     ; Y$ k9 m" |+ U' T6 U
     '2.Create a table
5 j% X7 ?- s' K3 k9 r     myCommand = New SqlCommand( "CREATE TABLE [test] ([id] [int] IDENTITY (1, 1) NOT NULL ,[name] % P' K7 N1 n; o& u  z" T
     0 T0 t8 C4 P: A
     [char] (10) COLLATE Chinese_PRC_CI_AS NULL ,[sex] [char] (10) COLLATE Chinese_PRC_CI_AS NULL
" t$ @4 o5 P; y# [% p6 ^     7 a7 T6 W6 `2 y# n* ]
     )", myConnection )
! M9 I0 z! b6 D- r     myCommand.ExecuteNonQuery() . O0 Q8 R; R- M8 ^; t7 ], G% R
     la2.text="New table created!" - ]& X% M5 m4 q& E* j: _7 h& G
     
" O7 \- R# f3 p* k# |3 M* G( K     '2 添加纪录
: e/ N7 C9 p9 n& t, a     myCommand = New SqlCommand( "Insert into [test] (name,sex) values( '黄志文','男' )",
" f5 P# K4 A- n     ( k4 |5 O4 F- r7 F
     myConnection ) ( }, R, m" F3 a" F. B
     myCommand.ExecuteNonQuery()
; Y* H3 m: n9 ^& {/ A     la3.text="New Record Inserted!" ! D- ^. Q$ S  c" {8 G# Q
     0 G& t2 K* Q9 l. x
     '3 更新数据 5 K3 v& t. k# B  q& ~5 @* q) L
     myCommand = New SqlCommand( "UPDATE [test] SET name='Smith' where name='李明'", myConnection ) & u( @0 s- s; T' A  I/ l
     myCommand.ExecuteNonQuery() ) a1 U6 e. I4 `% M
     la4.text="Record Updated!"
! l: I3 e0 J% z. r8 P     
$ k; H$ D. c+ L6 A     '4 删除数据
6 r( \: E5 l+ W) }& o& |5 V6 h& n     myCommand = New SqlCommand( "delete from [test] where name='Smith'", myConnection ) ) F2 k7 U3 S7 ]; D1 [+ U
     myCommand.ExecuteNonQuery() % n0 d: g) t2 F: U1 n
     la5.text="Record Deleted!" ! K1 m7 J# Z" ]
     
# O3 e2 I! \* T6 C     '5 用DataGrid显示数据
7 Y% b$ ]7 B* X  U0 v- V9 I     myCommand = New SqlCommand( "select * from [test]", myConnection )
6 O! w; r9 y  `4 {     MyDataGrid.DataSource=myCommand.ExecuteReader() % w1 o0 A1 J0 H+ Q
     MyDataGrid.DataBind() & s- @( `1 k& C* ]. D$ C, {
     end sub 5 z2 @7 C0 F( v! G4 D6 X; X, k/ ^$ @
     &lt;/script&gt; 9 V+ O8 k# T* z  [
     &lt;html&gt;
, o" S6 K& h  v. t- A; D     &lt;body&gt;
$ K! f, u8 s2 n# X+ ?, Y2 l6 l5 j     &lt;asp:label id="la1" runat="server" /&gt;&lt;br&gt; 8 O+ ?3 h8 v) i7 ?6 m6 ~5 a+ d
     &lt;asp:label id="la2" runat="server" /&gt;&lt;br&gt;
0 ^( I2 ~( z4 ?     &lt;asp:label id="la3" runat="server" /&gt;&lt;br&gt; , p# R4 H# D( k5 V# b( e, v1 W
     &lt;asp:label id="la4" runat="server" /&gt;&lt;br&gt;
" b+ K4 ?: J5 x: K$ f3 v# F  W  E     &lt;asp:label id="la5" runat="server" /&gt;&lt;br&gt; * a4 q. F' _9 g8 \
     &lt;ASPataGrid id="MyDataGrid" runat="server" % c6 A: s  [& P& X) P
     BorderColor="black"
8 s2 `  m" Y! x3 Q     BorderWidth="1"
$ V  y1 U/ r7 o- U0 U     GridLines="Both" / ^+ N  M6 l6 a: A3 p  C6 Q
     CellPadding="3"
* f) w( a6 `+ [. t- F     CellSpacing="0"
, w0 X9 y4 ?, o     Font-Name="Verdana"
5 t! J& k$ C! `, I; U4 F& B: u     Font-Size="10pt"
6 T( y6 ~# {" h+ s1 J* a6 b  Y     HeaderStyle-BackColor="#aaaadd" - V' u7 o) i1 ^3 f; x
     AlternatingItemStyle-BackColor="#eeeeee"   F' x# T: `; Q- s7 C
     &gt; ) T& k) i2 i7 _  h8 S
     &lt;/aspataGrid&gt;
9 g/ C' ~) e% u( e5 {     6 l0 Y7 u- \* ~2 K" k
     &lt;/body&gt; 3 r; v# ~: d( |% ~1 I
     &lt;/html&gt; / `: Y4 \# F' ]' c: Y& W
     -------------------------------------------------------------------------
6 c- D/ f5 g/ N+ E     推荐空间: $ U2 ^2 T1 S/ X( q" @. C( Y
     
# ^6 V7 `0 w1 R* S) b     ◆商务型C                送CN域名          1600元/年 + B1 B( f" ^; \$ k
     独立500网站空间 ,同时支持 ASP, ASP.NET;
% o$ `* u, Q0 T3 X9 r2 W     提供SQLSERVER2000的数据库存,支持ACCESS数据库
8 R' A# r8 L0 n* F" r1 R     送800MB邮箱邮箱
3 i! L3 y2 Q, \" C6 M, b" e* L
, Q% q; i; D1 R- k* @( J7 f* U     ◆虚拟专用C型机          送CN域名         4000元/年
& |; ?# y1 ^, W, o" V5 p4 A     3000M的网站空间,同时支持 ASP, ASP.NET
! D0 Y; ]- ~3 R. N* m: W$ c8 h     支持SQLSERVER2000/access数据库,数据库空间不限
  F, c) f8 N3 ^2 Q% z( O     送2000M VIP 企业邮局
- i: T0 ?& C# W/ k0 R     ------------------------------------------------------------------------ - H! \* ~* B- F% M% B
     同时有本月特大的优惠:
7 x" m2 ?' Q+ _" Z9 o# V     
. o/ v" L# }7 i' ?: o  N2 [! @     ◆增强C型主机      送CN域名          送300M 邮局        698 元/年 6 r* m4 S0 r/ o+ v1 b7 o, w# Q1 j. B$ m6 y
     ◆基本C型主机    送.CN或.COM域名     送100Mb邮局        348 元/年
5 |/ N+ i# Y/ d# p     ********************************************************************
- C! y$ y; @) a8 B0 A$ }0 n" o    ★购买或者续费时代互联的产品,产品7折大优惠!
+ @) l. U! V3 F: Z) m+ o# P      详情请见:http://now.net.cn/special/index_chris.net
! g: x" w0 S4 h1 R    ★.CN+地区.CN                 仅需180元!
# Z9 c; o. h% m9 t( Q' ?0 t      .Cn+VDNS无限+.COM+地区.CN   仅需228元!+ y5 F3 x5 M; Q: r; ?" E, _
     更多优惠,详情请见:http://now.net.cn/special/</P>
( G: \* I+ A. ]/ N+ Z; ~<>     全球免费咨询电话,请点击 <a href="http://todayisp.com/customer/moreline.net" target="_blank" >http://todayisp.com/customer/moreline.net</A> 0 q2 X% B+ A# R+ ?
     Q Q:188092185  342186899
- A0 T+ u# O$ t8 D0 T2 Y6 W     TEL:0756-2281071 </P>
作者: wy617958197    时间: 2014-9-4 20:48
谢谢谢谢。。




欢迎光临 数学建模社区-数学中国 (http://www.madio.net/) Powered by Discuz! X2.5