数学建模社区-数学中国

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

作者: rewd    时间: 2004-12-23 11:19
标题: ★Asp.net如何连接SQL Server2000数据库
<>★Asp.net如何连接SQL Server2000数据库
) Y& E, p- a1 j0 ^; `* d- W$ Y     7 V1 N$ K# u6 c/ R
     大家好,以下是有关ASP.net连接SQL Server2000数据库的例程,
3 W# m! Z/ }) ?! ^) O6 |     在这里和大家分享一下: " r- w' @; P$ D" ?$ v) U3 y
     
" d! k/ a4 u1 Q1 s     Asp.net连接SQL Server2000数据库例程详解: " L- s( {/ J9 l- }' U5 o
     &lt;%@ Import Namespace="System.Data" %&gt;
- a5 v6 U, v2 O     &lt;%@ Import NameSpace="System.Data.SqlClient" %&gt; 1 v1 \$ ?: A  M  F
     &lt;script laguage="VB" runat="server"&gt; ) ?" M! r; x% }5 G+ }
     sub page_load(sender as Object,e as EventArgs) , ~( M0 ^+ J: @, h
     Dim myConnection As SqlConnection
1 J2 ~/ b0 I' P6 h. Y     Dim myCommand As SqlCommand * X) }* ]4 m" T- d) ^( }  t
     Dim ds as DataSet
" v/ V+ @) b. v5 u5 p# m# E( a7 U     '1.connect to sql server $ a; Z5 h+ B# ]7 G  [8 p# T
     myConnection = New SqlConnection( "server=localhost;database=Pubs;uid=ueytjdf;pwd=doekdf" )   v% _  U' u( |8 b8 y( a
     myConnection.Open()
8 r6 K  ^4 T+ l3 t, h. G     la1.text="Connection Opened!" ; k: I8 @( L: w" d
     / V! a  g3 v* j$ @5 T+ X8 r
     '2.Create a table
" T% \0 c1 \6 N& a7 y7 h- S     myCommand = New SqlCommand( "CREATE TABLE [test] ([id] [int] IDENTITY (1, 1) NOT NULL ,[name]
. c& ^2 R4 F7 A# v, f     7 j* O0 G9 p0 i- q
     [char] (10) COLLATE Chinese_PRC_CI_AS NULL ,[sex] [char] (10) COLLATE Chinese_PRC_CI_AS NULL
' G) L* C; K/ i" k- I+ c     
, S( f0 g3 t% d& e7 q     )", myConnection ) 1 \% K, r, R' @; {! u
     myCommand.ExecuteNonQuery() . C5 ~# B+ z0 D) y
     la2.text="New table created!"
1 J( k$ A+ i# U     - I( o. m4 I) ~2 E6 s5 D$ n
     '2 添加纪录
2 G" H1 P' E) Q5 F3 Z* V2 l; e     myCommand = New SqlCommand( "Insert into [test] (name,sex) values( '黄志文','男' )",
8 n' x5 E* U' q  C     
1 t* B% d/ V$ O* I+ ~2 K* h     myConnection ) 8 K) i* H6 l3 c
     myCommand.ExecuteNonQuery() " U+ F( T/ `& U0 O
     la3.text="New Record Inserted!" 6 J3 [) G' n4 U# G
     4 E# h0 s- y. G8 |5 g
     '3 更新数据
0 B3 V2 X/ A; W4 G& v$ y2 i: ~     myCommand = New SqlCommand( "UPDATE [test] SET name='Smith' where name='李明'", myConnection )
2 g1 |0 g2 ~9 K0 o( F' m     myCommand.ExecuteNonQuery()
" L& j  V' z+ _' F     la4.text="Record Updated!"
) n7 v6 y, J+ `+ ~7 z7 J     
" _. a4 g0 I4 k& w# y, q     '4 删除数据
9 |$ q/ ], k: E; Z) @     myCommand = New SqlCommand( "delete from [test] where name='Smith'", myConnection ) ! b2 [" [1 S& H# u5 D
     myCommand.ExecuteNonQuery()
" J1 _. ~5 O+ p2 f) Y$ \     la5.text="Record Deleted!" $ R7 W: F. t7 x; k
     " l3 r" W! W; s/ t3 x
     '5 用DataGrid显示数据
+ ~- t3 R( ~# r4 ^     myCommand = New SqlCommand( "select * from [test]", myConnection ) . G4 G. x: ~4 g% B0 j
     MyDataGrid.DataSource=myCommand.ExecuteReader() / d: c; Z7 E) Y: [- e
     MyDataGrid.DataBind() ! l* o, a' b  V
     end sub
4 V' e% @  ?6 S     &lt;/script&gt; 2 ~7 {* x2 B1 ~0 @
     &lt;html&gt; ! ]: c- y: e8 |: i% o$ r  }
     &lt;body&gt; ) W4 M7 U( o6 u) e. ?8 m
     &lt;asp:label id="la1" runat="server" /&gt;&lt;br&gt; % l: s, i2 S# k
     &lt;asp:label id="la2" runat="server" /&gt;&lt;br&gt; $ ?# D6 @; i2 g+ y; c3 c% Y1 x8 m1 l
     &lt;asp:label id="la3" runat="server" /&gt;&lt;br&gt; 7 l8 D' ^! I1 S  C+ R( I  r6 Z
     &lt;asp:label id="la4" runat="server" /&gt;&lt;br&gt;
: W  n  N( ?* B. b     &lt;asp:label id="la5" runat="server" /&gt;&lt;br&gt; " H: g% X; u& R  |2 I
     &lt;ASPataGrid id="MyDataGrid" runat="server"
% L+ Y6 x& S5 s* B5 ]     BorderColor="black" 6 U8 U( Z4 r- C# v
     BorderWidth="1"   y; t4 ]# s% }: J
     GridLines="Both"
2 i1 h& }2 y  [     CellPadding="3"
- _, y  m% z+ M7 s     CellSpacing="0"
/ W% s+ h1 j2 c! K( O6 u$ s7 h/ V     Font-Name="Verdana" 7 ]. I# V3 \% |( o7 }
     Font-Size="10pt"
& x$ x- Y0 o" t% y: I     HeaderStyle-BackColor="#aaaadd" 5 d7 e6 c) B( U0 ]- O$ C
     AlternatingItemStyle-BackColor="#eeeeee"
9 i8 J. j4 n: y5 w6 p     &gt; 3 g- p/ a+ E2 a) ^" ~) d0 K
     &lt;/aspataGrid&gt;
6 A' J3 y0 P0 S$ _# s     
) o. i# b8 X6 u6 N; W     &lt;/body&gt;
1 Q* |: @9 y6 `: G$ o     &lt;/html&gt;
; _# r3 S, q8 }+ R: {     ------------------------------------------------------------------------- . ^# c& q# O! y4 n! c1 w6 ~6 z
     推荐空间:
2 A5 D) V$ @7 E; X% Q$ b% g     : j% p4 T; ^, S  _7 v: ]
     ◆商务型C                送CN域名          1600元/年
' E6 g7 q( u0 X- w$ M+ x! S     独立500网站空间 ,同时支持 ASP, ASP.NET; " H8 k7 [  [0 c& k. S( |
     提供SQLSERVER2000的数据库存,支持ACCESS数据库 2 y2 k. |1 f/ K5 s
     送800MB邮箱邮箱
" e* X2 p3 I% H. N' C- z1 C- v: K
6 {% D7 w) |! U; a. N: t; g     ◆虚拟专用C型机          送CN域名         4000元/年
) v, Q/ z6 C# Z3 d5 U     3000M的网站空间,同时支持 ASP, ASP.NET
8 ]1 T! w$ V5 T, @) ]# @4 y     支持SQLSERVER2000/access数据库,数据库空间不限
# H8 c+ K' Z1 _4 `) @9 T     送2000M VIP 企业邮局
1 g$ B& g& p# R. S& b     ------------------------------------------------------------------------
! M# H3 o# ~  Q7 q     同时有本月特大的优惠:
. b" ~' F5 {/ ]* T+ e9 @) S8 H% Q0 F     
0 x; s! c. C3 V( h2 _# Z     ◆增强C型主机      送CN域名          送300M 邮局        698 元/年
% C, i3 s, Q+ u$ Q# x     ◆基本C型主机    送.CN或.COM域名     送100Mb邮局        348 元/年
. }7 s) N7 j2 h3 q     ******************************************************************** ! t7 P4 F0 @0 a* p: ]
    ★购买或者续费时代互联的产品,产品7折大优惠!3 r8 x, c( V% _3 I
      详情请见:http://now.net.cn/special/index_chris.net
1 Z+ i5 O' |: \: w( m* y, R4 ^1 N    ★.CN+地区.CN                 仅需180元!2 K. a- W4 A* S2 d. }$ H1 h
      .Cn+VDNS无限+.COM+地区.CN   仅需228元!0 H$ i% s' ?" @% |) h
     更多优惠,详情请见:http://now.net.cn/special/</P>4 \3 |( p$ e" ], ~
<>     全球免费咨询电话,请点击 <a href="http://todayisp.com/customer/moreline.net" target="_blank" >http://todayisp.com/customer/moreline.net</A> - J6 x7 V, Z1 \9 m& t
     Q Q:188092185  342186899/ L5 j* i0 [. @3 Q7 [
     TEL:0756-2281071 </P>
作者: wy617958197    时间: 2014-9-4 20:48
谢谢谢谢。。




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