ASP.NET连接sql2008数据库的实现代码

2025-05-29 0 33

利用SqlConnection对象连接sql2000以上版本,并使用SqlCommand对象对数据库进行读取。

SqlCommand类概述:

用于对sql数据库执行sql语句或存储过程。

命名空间:System.Data.SqlClient

程序集: System.Data(在 System.Data.dll中)

SqlCommand类的属性

1.CommandText

获取或设置要对数据源执行的Transact—SQL语句或存储过程。

2. CommandType

获取或设置一个值,该值指示如何解释CommandText属性,CommandType默认为CommandType.Text,表示执行sql语句,调用存储过程时需设CommandType.StoredProcedure。3.Connection

获取或设置SqlCommand的实例使用的SqlConnection。

4.CommandTimeOut

获取或设置在终止执行命令的尝试并生成错误之前的等待时间。

SqlCommand类的方法

1.ExecuteNonQuery: 通过该命令执行不要返回值的操作,例如UPDATE,INSERT,DELETE等SQL命令,只是返回执行该命令所影响到表的行数。

2.ExecuteScalar: 可用来执行SELECT查询,但返回的是一个单一的值,用于查询聚合,例如使用count(), sum(),等函数的SQL指令。

3.ExecuteReader: 该方法返回一个DataReader对象,内容为查询结果的内容集合。

以下通过SqlConnection连接sql2008,并执行数据简单操作的代码:

  1. usingSystem;
  2. usingSystem.Collections.Generic;
  3. usingSystem.Linq;
  4. usingSystem.Web;
  5. usingSystem.Web.UI;
  6. usingSystem.Web.UI.WebControls;
  7. usingSystem.Data.SqlClient;
  8. usingSystem.Data;
  9. usingSystem.Configuration;
  10. publicpartialclass_Default:System.Web.UI.Page
  11. {
  12. protectedvoidPage_Load(objectsender,EventArgse)
  13. {
  14. //连接sql数据库
  15. Stringsqlconn="DataSource=SEEBRO-PC\\\\SQLEXPRESS;InitialCatalog=SuperMarket;IntegratedSecurity=True";
  16. SqlConnectionmyConnection=newSqlConnection(sqlconn);
  17. myConnection.Open();
  18. //定义SqlCommand类
  19. SqlCommandmyCommand=newSqlCommand();
  20. myCommand.Connection=myConnection;
  21. myCommand.CommandType=CommandType.StoredProcedure;
  22. myCommand.CommandText="bytype";
  23. //存储过程传参
  24. SqlParameterparInput=myCommand.Parameters.Add("@type",SqlDbType.SmallMoney);
  25. parInput.Direction=ParameterDirection.Input;
  26. parInput.Value=2;
  27. SqlDataReadermyReader=myCommand.ExecuteReader();
  28. Response.Write("<tableborder=1cellspaceing=0cellpadding=2>");
  29. Response.Write("<trbgcolor=#DAB4B>");
  30. for(inti=0;i<myReader.FieldCount;i++)
  31. Response.Write("<td>"+myReader.GetName(i)+"</td>");
  32. Response.Write("</tr>");
  33. while(myReader.Read())
  34. {
  35. Response.Write("<tr>");
  36. for(inti=0;i<myReader.FieldCount;i++)
  37. Response.Write("<td>"+myReader[i].ToString()+"</td>");
  38. Response.Write("</tr>");
  39. }
  40. Response.Write("</table>");
  41. myReader.Close();
  42. myConnection.Close();
  43. }
  44. }

改为执行sql指令后的代码,实现同样效果。

  1. usingSystem;
  2. usingSystem.Collections.Generic;
  3. usingSystem.Linq;
  4. usingSystem.Web;
  5. usingSystem.Web.UI;
  6. usingSystem.Web.UI.WebControls;
  7. usingSystem.Data.SqlClient;
  8. usingSystem.Data;
  9. usingSystem.Configuration;
  10. publicpartialclass_Default:System.Web.UI.Page
  11. {
  12. protectedvoidPage_Load(objectsender,EventArgse)
  13. {
  14. //连接sql数据库
  15. Stringsqlconn="DataSource=SEEBRO-PC\\\\SQLEXPRESS;InitialCatalog=SuperMarket;IntegratedSecurity=True";
  16. SqlConnectionmyConnection=newSqlConnection(sqlconn);
  17. myConnection.Open();
  18. //定义SqlCommand类
  19. SqlCommandmyCommand=newSqlCommand("select*fromProductwhereProduct.价格=2",myConnection);
  20. SqlDataReadermyReader=myCommand.ExecuteReader();
  21. Response.Write("<tableborder=1cellspaceing=0cellpadding=2>");
  22. Response.Write("<trbgcolor=#DAB4B>");
  23. for(inti=0;i<myReader.FieldCount;i++)
  24. Response.Write("<td>"+myReader.GetName(i)+"</td>");
  25. Response.Write("</tr>");
  26. while(myReader.Read())
  27. {
  28. Response.Write("<tr>");
  29. for(inti=0;i<myReader.FieldCount;i++)
  30. Response.Write("<td>"+myReader[i].ToString()+"</td>");
  31. Response.Write("</tr>");
  32. }
  33. Response.Write("</table>");
  34. myReader.Close();
  35. myConnection.Close();
  36. }
  37. }

运行效果:

ASP.NET连接sql2008数据库的实现代码

项目代码已上传。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 ASP.NET连接sql2008数据库的实现代码 https://www.kuaiidc.com/103335.html

相关文章

发表评论
暂无评论