QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 5129|回复: 1
打印 上一主题 下一主题

★Asp.net如何连接SQL Server2000数据库

[复制链接]
字体大小: 正常 放大
rewd        

1

主题

2

听众

36

积分

升级  32.63%

该用户从未签到

新人进步奖

跳转到指定楼层
1#
发表于 2004-12-23 11:19 |只看该作者 |倒序浏览
|招呼Ta 关注Ta
<>★Asp.net如何连接SQL Server2000数据库0 [4 c; ]5 W7 I+ V8 f5 S
     
- N- r% |" d0 ~     大家好,以下是有关ASP.net连接SQL Server2000数据库的例程, 1 E, @; U$ @9 B+ @* F( h0 K' u
     在这里和大家分享一下:
# l( S; ]" N# U( }" i5 }3 L     2 K2 y7 M# A) T0 o: [- l+ ~
     Asp.net连接SQL Server2000数据库例程详解:
% _, m9 H8 U9 _, P$ Y7 D! C+ E     &lt;%@ Import Namespace="System.Data" %&gt; $ a3 L: B6 g: V' @6 Y
     &lt;%@ Import NameSpace="System.Data.SqlClient" %&gt;
/ g( S+ W3 Z+ \, r     &lt;script laguage="VB" runat="server"&gt;
/ L& w6 V6 a7 h     sub page_load(sender as Object,e as EventArgs)
& e! h$ T1 W# f' ]9 s7 q3 k2 k     Dim myConnection As SqlConnection ; `4 c$ B& t8 ^' p+ Z% P
     Dim myCommand As SqlCommand
' v% T7 h4 ?& j1 F2 D8 t/ ?     Dim ds as DataSet * [8 C7 @7 O! R. ~5 h' ]' Q
     '1.connect to sql server 0 b" j) p/ U8 P2 S3 w
     myConnection = New SqlConnection( "server=localhost;database=Pubs;uid=ueytjdf;pwd=doekdf" )
* y9 J: h9 r7 m5 g' P     myConnection.Open()
5 y5 B6 F4 v( j0 n4 A     la1.text="Connection Opened!"
7 n' G* k$ |. h% `8 c+ U: m! O     ) l& R) {/ _+ {2 e, V
     '2.Create a table
+ z. `' ^5 J8 V/ h     myCommand = New SqlCommand( "CREATE TABLE [test] ([id] [int] IDENTITY (1, 1) NOT NULL ,[name]
/ D' X/ k& W- ^! T/ D     ( L7 g: y" H! N- X& e
     [char] (10) COLLATE Chinese_PRC_CI_AS NULL ,[sex] [char] (10) COLLATE Chinese_PRC_CI_AS NULL % ^* F4 L* x# S2 O9 s
     
0 ~% Z( g0 a- G, ^. `$ ^: P9 _     )", myConnection )
! x- j$ r* P. @/ s/ `. O     myCommand.ExecuteNonQuery()
" F& n* `' j5 u/ L. _     la2.text="New table created!"
) b( `  D, I5 e/ \( |2 Y     
, U, `. [% s& m9 B     '2 添加纪录 / G; a4 a8 H2 K5 G' ]
     myCommand = New SqlCommand( "Insert into [test] (name,sex) values( '黄志文','男' )",
& A" {$ c" Y) q, h: ?3 x     " k8 D( ?/ e1 W- r$ H
     myConnection ) ! [9 c) d8 p8 U4 n$ W' Z4 A$ I, U4 Y% V
     myCommand.ExecuteNonQuery()
6 J1 x: C. U; w2 X% J& n     la3.text="New Record Inserted!" # H. K- _6 ?% y7 o+ Z; U* K! d' H+ {
     
5 \1 m' d& D6 v( Y$ f) S     '3 更新数据 ) {  r/ }% E/ |
     myCommand = New SqlCommand( "UPDATE [test] SET name='Smith' where name='李明'", myConnection ) ' R; F: G6 q2 j$ J* y! ^
     myCommand.ExecuteNonQuery() ; Q- U4 D5 ^% `9 c" F2 {
     la4.text="Record Updated!"
% C! ]/ C7 h* I) j& k     
: `% S0 \; ], |" ^% d     '4 删除数据
3 a' ]& W2 w- A4 i, U     myCommand = New SqlCommand( "delete from [test] where name='Smith'", myConnection ) 1 x3 V1 v4 o, v/ f% D% {' E& T/ e
     myCommand.ExecuteNonQuery()
* {" g# o6 A, ?2 g* z; S     la5.text="Record Deleted!" . D+ J) i, N9 o1 O$ ]
     6 {, O8 \0 x0 J/ y4 D
     '5 用DataGrid显示数据 7 t5 H8 T- G) Q$ }  y
     myCommand = New SqlCommand( "select * from [test]", myConnection ) 8 x% S* x1 C. r7 H
     MyDataGrid.DataSource=myCommand.ExecuteReader() ; s& J3 L4 t9 o* A, z4 e3 L( r
     MyDataGrid.DataBind()
/ g! C5 e2 S4 F4 J     end sub + k2 o; u# F  n4 C9 p  l
     &lt;/script&gt; " p: p0 ?. u8 X  m$ O/ ?
     &lt;html&gt; * T5 d  {# \  u& h  M- O1 H
     &lt;body&gt;
/ y4 [* a) y# m  b     &lt;asp:label id="la1" runat="server" /&gt;&lt;br&gt;
5 h* W  U2 p/ m5 w* l6 U, A& n3 U     &lt;asp:label id="la2" runat="server" /&gt;&lt;br&gt; ; l0 N/ e! F/ L. k3 p, r" I
     &lt;asp:label id="la3" runat="server" /&gt;&lt;br&gt; - }) e8 }& A8 P
     &lt;asp:label id="la4" runat="server" /&gt;&lt;br&gt;
: V3 w0 j' z9 G$ A( d     &lt;asp:label id="la5" runat="server" /&gt;&lt;br&gt;
8 z6 x& N. ^/ F0 H# q: K2 c     &lt;ASPataGrid id="MyDataGrid" runat="server" 7 g, R0 t% ?2 m8 a9 D+ f/ g
     BorderColor="black" 7 _" O7 x: S+ r  T' x4 D
     BorderWidth="1" $ M- `# R# u- t
     GridLines="Both"
% Y( ^; U6 d7 S" Y$ ], n     CellPadding="3" " E* }/ h! G& M9 X3 \8 x4 }
     CellSpacing="0"
/ b8 |! j5 I4 e0 ]     Font-Name="Verdana" $ [, C& [( I% M* U7 J+ b
     Font-Size="10pt" 2 r: g9 D* J( F4 q6 J# q
     HeaderStyle-BackColor="#aaaadd"
" P5 h3 ~. B; D( {     AlternatingItemStyle-BackColor="#eeeeee"
, x0 W! d8 b; P9 ]$ ?0 z* o     &gt; % M9 ]% \& z/ U' n" O% M  G/ Z
     &lt;/aspataGrid&gt;
3 }, g& y/ L+ d7 y. ?  ^9 J     ( Q: [& A, I& h% g: _2 d2 E+ `( G
     &lt;/body&gt;   U: c9 h6 O0 h) O( P7 }
     &lt;/html&gt; . I( @& I8 b3 e; P5 x% R2 w
     ------------------------------------------------------------------------- 9 h! V, r- h8 v' q+ h* ~; M% W
     推荐空间:
9 ^: s3 G. L4 e- z- n/ Z8 w% d$ L     $ F4 N2 y3 j5 b3 @# H$ b6 J
     ◆商务型C                送CN域名          1600元/年 ) P* ?+ O. {( Q' `1 B. n1 x
     独立500网站空间 ,同时支持 ASP, ASP.NET; ' l1 V! ~$ B6 |* j
     提供SQLSERVER2000的数据库存,支持ACCESS数据库
# U7 }! h8 Z3 [3 C$ `     送800MB邮箱邮箱
) k4 S+ Y7 E& }4 d ; Q! r. ?6 B( B5 f4 v3 M; |
     ◆虚拟专用C型机          送CN域名         4000元/年
: {: t& q$ T& k( m" d/ T) t# M4 V     3000M的网站空间,同时支持 ASP, ASP.NET
( a/ h2 }' k0 m/ b' l     支持SQLSERVER2000/access数据库,数据库空间不限
6 B7 k9 D' k( W8 O3 x8 t     送2000M VIP 企业邮局 , c0 @3 K4 N4 W2 w5 q& M2 d
     ------------------------------------------------------------------------
, K1 G' d. d$ e7 [6 O$ _     同时有本月特大的优惠:
: M- }0 h; V/ \; _. _% m; X     & F2 e# V+ C: A
     ◆增强C型主机      送CN域名          送300M 邮局        698 元/年
! d7 S7 z  _0 u+ K" {     ◆基本C型主机    送.CN或.COM域名     送100Mb邮局        348 元/年 9 i# u, m& b; n$ }- Q! O& L
     ********************************************************************
1 y8 X; `# Q: H+ V    ★购买或者续费时代互联的产品,产品7折大优惠!; \. U1 H9 }. Q
      详情请见:http://now.net.cn/special/index_chris.net* V! t+ q/ Q" D: d2 o* ?- p. w
    ★.CN+地区.CN                 仅需180元!
. r  t+ k+ G6 v      .Cn+VDNS无限+.COM+地区.CN   仅需228元!' {7 U4 A4 |2 ~( b
     更多优惠,详情请见:http://now.net.cn/special/</P>
( b/ X' V. G/ {" ^! @7 g' K<>     全球免费咨询电话,请点击 <a href="http://todayisp.com/customer/moreline.net" target="_blank" >http://todayisp.com/customer/moreline.net</A> & @. N' s/ r3 S, _5 j# V) M. V! g# N1 X
     Q Q:188092185  342186899
: P' x. s" ~( T  Y( s8 X4 f% L     TEL:0756-2281071 </P>
zan
转播转播0 分享淘帖0 分享分享0 收藏收藏0 支持支持0 反对反对0 微信微信

2

主题

15

听众

759

积分

升级  39.75%

  • TA的每日心情
    开心
    2015-8-26 15:55
  • 签到天数: 39 天

    [LV.5]常住居民I

    群组学术交流B

    群组2014数学建模国赛备战

    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 注册地址

    qq
    收缩
    • 电话咨询

    • 04714969085
    fastpost

    关于我们| 联系我们| 诚征英才| 对外合作| 产品服务| QQ

    手机版|Archiver| |繁體中文 手机客户端  

    蒙公网安备 15010502000194号

    Powered by Discuz! X2.5   © 2001-2013 数学建模网-数学中国 ( 蒙ICP备14002410号-3 蒙BBS备-0002号 )     论坛法律顾问:王兆丰

    GMT+8, 2026-8-23 05:48 , Processed in 0.336227 second(s), 56 queries .

    回顶部