发布于 2016-06-25 06:47:45 | 177 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

ASP.NET

ASP.NET 是.NET FrameWork的一部分,是一项微软公司的技术,是一种使嵌入网页中的脚本可由因特网服务器执行的服务器端脚本技术,它可以在通过HTTP请求文档时再在Web服务器上动态创建它们。 指 Active Server Pages(动态服务器页面) ,运行于 IIS(Internet Information Server 服务,是Windows开发的Web服务器)之中的程序 。


这篇文章主要介绍了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,并执行数据简单操作的代码:


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

运行效果:

项目代码已上传。



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务