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

(编辑:jimmy 日期: 2024/10/15 浏览:2)

利用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连接sql2008数据库的实现代码

项目代码已上传。

一句话新闻

一文看懂荣耀MagicBook Pro 16
荣耀猎人回归!七大亮点看懂不只是轻薄本,更是游戏本的MagicBook Pro 16.
人们对于笔记本电脑有一个固有印象:要么轻薄但性能一般,要么性能强劲但笨重臃肿。然而,今年荣耀新推出的MagicBook Pro 16刷新了人们的认知——发布会上,荣耀宣布猎人游戏本正式回归,称其继承了荣耀 HUNTER 基因,并自信地为其打出“轻薄本,更是游戏本”的口号。
众所周知,寻求轻薄本的用户普遍更看重便携性、外观造型、静谧性和打字办公等用机体验,而寻求游戏本的用户则普遍更看重硬件配置、性能释放等硬核指标。把两个看似难以相干的产品融合到一起,我们不禁对它产生了强烈的好奇:作为代表荣耀猎人游戏本的跨界新物种,它究竟做了哪些平衡以兼顾不同人群的各类需求呢?