ASP.NET数据库连接实例浅析


  本文标签:ASP.NET数据库连接

  ASP.NET数据库连接实例展现:

  1.  
  2. using System;   
  3. using System.Collections;   
  4. using System.ComponentModel;   
  5. using System.Data;   
  6. using System.Drawing;   
  7. using System.Web;   
  8. using System.Web.SessionState;   
  9. using System.Web.UI;   
  10. using System.Web.UI.WebControls;   
  11. using System.Web.UI.HTMLControls;   
  12. using System.Data.SqlClient;   
  13.  
  14. namespace Example01   
  15. {   
  16. ///   
  17. /// WebForm1 的摘要说明  。   
  18. ///   
  19. public class WebForm1 : System.Web.UI.Page   
  20. {   
  21. private void Page_Load(object sender, System.EventArgs e)   
  22. {   
  23. // 在此处放置用户代码以初始化页面   
  24. Response.Write("   
  25.  
  26. 第一个ASP.NET数据库连接的实例   
  27.  
  28. ");   
  29. SqlConnection myConnection = new SqlConnection("server=localhost;uid=sa;pwd=sa");   
  30. try   
  31. {   
  32. myConnection.Open();//打开数据库链接   
  33. Response.Write("链接成功!");   
  34. }   
  35. catch   
  36. {   
  37. Response.Write("链接失败!");   
  38. }   
  39.  
  40.  
  41. #region Web 窗体设计器生成的代码   
  42. override protected void OnInit(EventArgs e)   
  43. {   
  44. //   
  45. // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的  。   
  46. //   
  47. InitializeComponent();   
  48. base.OnInit(e);   
  49. }   
  50.  
  51. ///   
  52. /// ASP.NET数据库连接之设计器支持所需的方法 - 不要使用代码编辑器修改   
  53. /// 此方法的内容  。   
  54. ///   
  55. private void InitializeComponent()   
  56. {   
  57. this.Load += new System.EventHandler(this.Page_Load);   
  58.  
  59. #endregion   
  60. }   

  ASP.NET数据库连接的实例就向你介绍到这里,希望对你有所帮助  。