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

Valentina ·
更新时间:2024-09-21
· 872 次阅读

利用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(); } }

运行效果:

项目代码已上传。

您可能感兴趣的文章:asp.net 使用ObjectDataSource控件在ASP.NET中实现Ajax真分页动态指定任意类型的ObjectDataSource对象的查询参数asp.net中将数据库绑定到DataList控件的实现方法与实例代码gridview+objectdatasource+aspnetpager整合实例ASP.NET web.config中数据库连接字符串connectionStrings节的配置方法ASP.NET中操作SQL数据库(连接字符串的配置及获取)ASP.NET连接MySql数据库的2个方法及示例ASP.NET连接数据库并获取数据方法总结ASP.NET中 ObjectDataSource控件的DataObjectTypeName属性



ASP.NET net SQL 数据 数据库 ASP

需要 登录 后方可回复, 如果你还没有账号请 注册新账号