利用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,并执行数据简单操作的代码:
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Data.SqlClient;using System.Data;using System.Configuration;public partial class _Default : System.Web.UI.Page{protected void Page_Load(object sender, EventArgs e){// 连接sql数据库String sqlconn = "Data Source=SEEBRO-PC\SQLEXPRESS;Initial Catalog=SuperMarket;Integrated Security=True";SqlConnection myConnection = new SqlConnection(sqlconn);myConnection.Open();//定义SqlCommand类SqlCommand myCommand = new SqlCommand();myCommand.Connection = myConnection;myCommand.CommandType = CommandType.StoredProcedure;myCommand.CommandText = "bytype";//存储过程传参SqlParameter parInput = myCommand.Parameters.Add("@type", SqlDbType.SmallMoney);parInput.Direction = ParameterDirection.Input;parInput.Value = 2;SqlDataReader myReader = myCommand.ExecuteReader();Response.Write("<table border=1 cellspaceing=0 cellpadding=2>");Response.Write("<tr bgcolor=#DAB4B>");for (int i = 0; i < myReader.FieldCount; i++)Response.Write("<td>" + myReader.GetName(i) + "</td>");Response.Write("</tr>");while (myReader.Read()){Response.Write("<tr>");for (int i = 0; i < myReader.FieldCount; i++)Response.Write("<td>" + myReader[i].ToString() + "</td>");Response.Write("</tr>");}Response.Write("</table>");myReader.Close();myConnection.Close();}}改为执行sql指令后的代码,实现同样效果。
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Data.SqlClient;using System.Data;using System.Configuration;public partial class _Default : System.Web.UI.Page{protected void Page_Load(object sender, EventArgs e){// 连接sql数据库String sqlconn = "Data Source=SEEBRO-PC\SQLEXPRESS;Initial Catalog=SuperMarket;Integrated Security=True";SqlConnection myConnection = new SqlConnection(sqlconn);myConnection.Open();//定义SqlCommand类SqlCommand myCommand = new SqlCommand("select * from Product where Product.价格 = 2", myConnection);SqlDataReader myReader = myCommand.ExecuteReader();Response.Write("<table border=1 cellspaceing=0 cellpadding=2>");Response.Write("<tr bgcolor=#DAB4B>");for (int i = 0; i < myReader.FieldCount; i++)Response.Write("<td>" + myReader.GetName(i) + "</td>");Response.Write("</tr>");while (myReader.Read()){Response.Write("<tr>");for (int i = 0; i < myReader.FieldCount; i++)Response.Write("<td>" + myReader[i].ToString() + "</td>");Response.Write("</tr>");}Response.Write("</table>");myReader.Close();myConnection.Close();}}运行效果:

项目代码已上传。