服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服务器之家 - 编程语言 - ASP.NET教程 - ASP.NET连接sql2008数据库的实现代码

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

2019-12-13 10:01mdxy-dxy ASP.NET教程

这篇文章主要介绍了ASP.NET连接sql2008数据库的实现代码,需要的朋友可以参考下

利用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. using System; 
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Web; 
  5. using System.Web.UI; 
  6. using System.Web.UI.WebControls; 
  7.   
  8. using System.Data.SqlClient; 
  9. using System.Data; 
  10. using System.Configuration; 
  11.   
  12. public partial class _Default : System.Web.UI.Page 
  13.   protected void Page_Load(object sender, EventArgs e) 
  14.   { 
  15.     // 连接sql数据库 
  16.     String sqlconn = "Data Source=SEEBRO-PC\\SQLEXPRESS;Initial Catalog=SuperMarket;Integrated Security=True"
  17.     SqlConnection myConnection = new SqlConnection(sqlconn); 
  18.     myConnection.Open(); 
  19.   
  20.     //定义SqlCommand类 
  21.     SqlCommand myCommand = new SqlCommand(); 
  22.     myCommand.Connection = myConnection; 
  23.     myCommand.CommandType = CommandType.StoredProcedure; 
  24.     myCommand.CommandText = "bytype"
  25.     //存储过程传参 
  26.     SqlParameter parInput = myCommand.Parameters.Add("@type", SqlDbType.SmallMoney); 
  27.     parInput.Direction = ParameterDirection.Input; 
  28.     parInput.Value = 2; 
  29.   
  30.     SqlDataReader myReader = myCommand.ExecuteReader(); 
  31.   
  32.     Response.Write("<table border=1 cellspaceing=0 cellpadding=2>"); 
  33.     Response.Write("<tr bgcolor=#DAB4B>"); 
  34.     for (int i = 0; i < myReader.FieldCount; i++) 
  35.       Response.Write("<td>" + myReader.GetName(i) + "</td>"); 
  36.     Response.Write("</tr>"); 
  37.   
  38.     while (myReader.Read()) 
  39.     { 
  40.       Response.Write("<tr>"); 
  41.       for (int i = 0; i < myReader.FieldCount; i++) 
  42.         Response.Write("<td>" + myReader[i].ToString() + "</td>"); 
  43.       Response.Write("</tr>"); 
  44.     } 
  45.     Response.Write("</table>"); 
  46.   
  47.     myReader.Close(); 
  48.     myConnection.Close(); 
  49.   } 

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

  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Web; 
  5. using System.Web.UI; 
  6. using System.Web.UI.WebControls; 
  7.   
  8. using System.Data.SqlClient; 
  9. using System.Data; 
  10. using System.Configuration; 
  11.   
  12. public partial class _Default : System.Web.UI.Page 
  13.   protected void Page_Load(object sender, EventArgs e) 
  14.   { 
  15.     // 连接sql数据库 
  16.     String sqlconn = "Data Source=SEEBRO-PC\\SQLEXPRESS;Initial Catalog=SuperMarket;Integrated Security=True"
  17.     SqlConnection myConnection = new SqlConnection(sqlconn); 
  18.     myConnection.Open(); 
  19.   
  20.     //定义SqlCommand类 
  21.     SqlCommand myCommand = new SqlCommand("select * from Product where Product.价格 = 2", myConnection); 
  22.     SqlDataReader myReader = myCommand.ExecuteReader(); 
  23.   
  24.     Response.Write("<table border=1 cellspaceing=0 cellpadding=2>"); 
  25.     Response.Write("<tr bgcolor=#DAB4B>"); 
  26.     for (int i = 0; i < myReader.FieldCount; i++) 
  27.       Response.Write("<td>" + myReader.GetName(i) + "</td>"); 
  28.     Response.Write("</tr>"); 
  29.   
  30.     while (myReader.Read()) 
  31.     { 
  32.       Response.Write("<tr>"); 
  33.       for (int i = 0; i < myReader.FieldCount; i++) 
  34.         Response.Write("<td>" + myReader[i].ToString() + "</td>"); 
  35.       Response.Write("</tr>"); 
  36.     } 
  37.     Response.Write("</table>"); 
  38.   
  39.     myReader.Close(); 
  40.     myConnection.Close(); 
  41.   } 

运行效果:

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

项目代码已上传。

延伸 · 阅读

精彩推荐