数学建模社区-数学中国

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

作者: rewd    时间: 2004-12-23 11:19
标题: ★Asp.net如何连接SQL Server2000数据库
<>★Asp.net如何连接SQL Server2000数据库% n! o8 A4 {' _/ `
     " M* H% i: P! F( D; l
     大家好,以下是有关ASP.net连接SQL Server2000数据库的例程, / C5 H% {, Z8 x
     在这里和大家分享一下: # K& w/ I+ A( u0 x$ `2 G
     % {2 M4 Q5 Z7 y0 d
     Asp.net连接SQL Server2000数据库例程详解: . W: m0 \! H8 \; \
     &lt;%@ Import Namespace="System.Data" %&gt;
; e. H( g  }' K7 C8 H; j: U     &lt;%@ Import NameSpace="System.Data.SqlClient" %&gt;
, y7 U% b3 ?' W1 b' @     &lt;script laguage="VB" runat="server"&gt;
" u7 K! U5 W0 g$ S) I     sub page_load(sender as Object,e as EventArgs) 8 E9 c( A  A, |# w
     Dim myConnection As SqlConnection
" Q& y2 _7 S6 r4 i2 j% [/ V     Dim myCommand As SqlCommand
% Q# \3 K9 M7 X( {7 k# d4 U+ K     Dim ds as DataSet
; d" \6 ~7 n$ K" L0 V, b% H     '1.connect to sql server
" x7 b' Q4 J3 q5 k( \+ }  Q# I! v- |( v     myConnection = New SqlConnection( "server=localhost;database=Pubs;uid=ueytjdf;pwd=doekdf" ) : m5 P, y, Y1 U# P
     myConnection.Open() ) d- T8 S- R8 v4 x! i5 G
     la1.text="Connection Opened!" 8 c- C$ Q6 {3 Q& Z! B! p% U
     
' P( O$ Q9 M: b. r# d4 z     '2.Create a table . ]+ H3 V7 H# K. o9 E
     myCommand = New SqlCommand( "CREATE TABLE [test] ([id] [int] IDENTITY (1, 1) NOT NULL ,[name]
/ D; Z1 M* @2 }& J5 X     
* ]/ w; n" v2 y* g( P     [char] (10) COLLATE Chinese_PRC_CI_AS NULL ,[sex] [char] (10) COLLATE Chinese_PRC_CI_AS NULL
) r2 h: E* u  I  P$ m( I. W0 R     # F2 D8 U" \0 y% B
     )", myConnection )
: C6 Z, E7 ^* f7 T/ {     myCommand.ExecuteNonQuery()
( f) O6 i1 m6 ]; k0 f3 n     la2.text="New table created!"   e: y% \2 F) Z# r: `/ M
     ( K. t/ W7 p6 l3 N* h
     '2 添加纪录 ! s4 p$ Y: K/ B/ c8 O
     myCommand = New SqlCommand( "Insert into [test] (name,sex) values( '黄志文','男' )", 0 \7 F5 H5 w- n3 u/ v# Z# x6 r
     # l2 q6 N; ^9 ^# |! I! I. Y
     myConnection )   F: T( o2 u# S: C0 ?. N7 k, q
     myCommand.ExecuteNonQuery()
. d2 d. G8 w* Q) j/ G! \& {+ y1 D  Z5 K     la3.text="New Record Inserted!"
% }) o# {8 h) |; ^2 \     ; S; Y- K9 L, h  F' g. M; K. L0 R/ B
     '3 更新数据
7 |- }: C; E$ D, h- E     myCommand = New SqlCommand( "UPDATE [test] SET name='Smith' where name='李明'", myConnection )
; b: Y5 q. r- w! ^% t3 k     myCommand.ExecuteNonQuery() + o' [& _1 x; r7 m" w3 _
     la4.text="Record Updated!"
' z7 S% {9 D( @6 e# j     - e" n+ v& e; j+ Z
     '4 删除数据 . o+ T+ z8 t- V- D  i
     myCommand = New SqlCommand( "delete from [test] where name='Smith'", myConnection ) / I/ u' r8 G& F/ J9 N
     myCommand.ExecuteNonQuery()
2 m0 C- `1 z) Z3 A# N! f* K     la5.text="Record Deleted!" 3 D3 Y2 z- t- X
     
9 H2 x9 D" B4 f/ t- S! x  D; ?     '5 用DataGrid显示数据
6 U/ L3 \! O; r6 `% Y# F6 n! l     myCommand = New SqlCommand( "select * from [test]", myConnection )
1 |2 B9 P# y& B& v# J1 R1 d# I     MyDataGrid.DataSource=myCommand.ExecuteReader() ) G1 G) x8 {! E
     MyDataGrid.DataBind()
4 z; A% W3 Z3 U4 j, n" c     end sub ( D; D8 t- A" P! A% @1 T/ [0 D# b3 O
     &lt;/script&gt; - t9 U5 h* R/ T
     &lt;html&gt;
& F- }$ ]0 t# V% X4 }; m     &lt;body&gt;
0 D3 U; P1 X) v8 v& ^3 V, B     &lt;asp:label id="la1" runat="server" /&gt;&lt;br&gt;
0 g% M% W3 o5 c     &lt;asp:label id="la2" runat="server" /&gt;&lt;br&gt; - c6 u: F9 n6 Q# {3 w1 f
     &lt;asp:label id="la3" runat="server" /&gt;&lt;br&gt;
7 k, v7 N# @) Q' O# _: R" U     &lt;asp:label id="la4" runat="server" /&gt;&lt;br&gt;
3 k/ z; O( d& L* ^     &lt;asp:label id="la5" runat="server" /&gt;&lt;br&gt;
  e$ n' m3 ~  z! E: @& s     &lt;ASPataGrid id="MyDataGrid" runat="server" 7 `& |% u( [0 p: ]' z, W% T
     BorderColor="black"   u: y9 r" t8 ~9 D# _3 e
     BorderWidth="1"
" E/ K: i& f5 h6 s; g     GridLines="Both"
* |1 y2 ?7 b* m0 K7 y     CellPadding="3"
, ?% U' J. v( U     CellSpacing="0" 2 |5 f: j6 v* v2 I; Q& H( q- o
     Font-Name="Verdana" - D& ~6 N& l4 P) x: q2 ?
     Font-Size="10pt" 4 m* K1 |; P* h9 s, p; ?
     HeaderStyle-BackColor="#aaaadd"
' U7 o/ {  i  i$ a# D     AlternatingItemStyle-BackColor="#eeeeee"
* g+ X) E! d0 r- P+ F( B     &gt;
; F8 ]+ R  Y3 m8 q1 m" l     &lt;/aspataGrid&gt;
( _0 K/ K/ G1 z& j; g     ! I0 ]5 E! m. \- h
     &lt;/body&gt;
4 M" |6 W" S- H7 N: g! @3 U5 m     &lt;/html&gt; / D% ^4 q- i8 S: C, |
     ------------------------------------------------------------------------- , Y, `7 F1 {2 u' @! i
     推荐空间:
4 X4 ~  T  d( S6 {6 v% Y     
: t9 r+ N6 L6 [1 z' L( a; F2 W5 b     ◆商务型C                送CN域名          1600元/年 " a5 \3 I" R$ {# l" K* d4 X
     独立500网站空间 ,同时支持 ASP, ASP.NET; 0 r: T8 f0 C' [3 _6 V5 H
     提供SQLSERVER2000的数据库存,支持ACCESS数据库 4 ]9 i  L7 T# R3 m
     送800MB邮箱邮箱 4 l8 O  M, `8 e% F) }

, z; K( K. C8 C     ◆虚拟专用C型机          送CN域名         4000元/年 : A. B1 w  E3 N% H: f$ X+ k
     3000M的网站空间,同时支持 ASP, ASP.NET % m( t4 \. i3 Y
     支持SQLSERVER2000/access数据库,数据库空间不限 : W7 ^& d/ \* a
     送2000M VIP 企业邮局 ' Q$ @4 B; s4 \6 t1 N( g- f8 Q" @
     ------------------------------------------------------------------------
& x1 C7 R5 I1 l4 F     同时有本月特大的优惠:
4 N: `! l! |/ V9 Q3 }     - z7 ?3 ?- J+ h! J' n
     ◆增强C型主机      送CN域名          送300M 邮局        698 元/年
' y5 E2 a6 d8 g     ◆基本C型主机    送.CN或.COM域名     送100Mb邮局        348 元/年 . c2 O1 O. e; p' @' g- N2 F# n
     ******************************************************************** : L4 U1 d8 ^5 [. a
    ★购买或者续费时代互联的产品,产品7折大优惠!
$ l6 Q9 U( M) t: R      详情请见:http://now.net.cn/special/index_chris.net
% Z9 S6 G5 V- n) z' f( ]    ★.CN+地区.CN                 仅需180元!9 c# Q; m4 A7 C$ w" }
      .Cn+VDNS无限+.COM+地区.CN   仅需228元!! e: n8 x: [* i. U2 i' \+ R
     更多优惠,详情请见:http://now.net.cn/special/</P>
- H: U2 P3 U3 S<>     全球免费咨询电话,请点击 <a href="http://todayisp.com/customer/moreline.net" target="_blank" >http://todayisp.com/customer/moreline.net</A>
! @9 i; F- k$ G6 F: ]6 H  Z/ l2 H  v7 ?     Q Q:188092185  342186899
2 O( m% \1 Z2 Z: H, V) I6 c" O     TEL:0756-2281071 </P>
作者: wy617958197    时间: 2014-9-4 20:48
谢谢谢谢。。




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