当前位置: 首页 > 编程笔记 >

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

燕志学
2023-03-14
本文向大家介绍ASP.NET连接sql2008数据库的实现代码,包括了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();
  }
}

运行效果:

项目代码已上传。

 类似资料:
  • 本文向大家介绍VB6实现连接Access数据库的ADODB代码实现方法,包括了VB6实现连接Access数据库的ADODB代码实现方法的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了使用ADODB.Connection连接access数据库的方法,驱动类型版本为:Microsoft.Jet.OLEDB.4.0。在VB的数据库操作中,连接数据库是第一步,也是最基本的,本文所述的这个例子,对于

  • 本文向大家介绍使用Nodejs连接mongodb数据库的实现代码,包括了使用Nodejs连接mongodb数据库的实现代码的使用技巧和注意事项,需要的朋友参考一下 一个简单的nodejs连接mongodb示例,来自 mongodb官方示例 1. 创建package.json 首先,创建我们的工程目录connect-mongodb,并作为我们的当前目录 输入npm init命令创建package.j

  • 本文向大家介绍ASP.NET oledb连接Access数据库的方法,包括了ASP.NET oledb连接Access数据库的方法的使用技巧和注意事项,需要的朋友参考一下 使用OleDBCommand相关操作类需要引入System.Data.OleDb命名空间。 通过连接一个带密码的access数据库 读取其中的数据并置入表格中显示,验证连接和命令使用正确性。 效果图如下: 完整项目文件已上传。

  • 本文向大家介绍PHP实现的sqlite数据库连接类,包括了PHP实现的sqlite数据库连接类的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了PHP实现的sqlite数据库连接类。分享给大家供大家参考。具体实现方法如下: 该sqlite数据库连接类就是利用了php与sqlite进行连接操作,代码如下: 希望本文所述对大家的PHP数据库程序设计有所帮助。

  • 本文向大家介绍IDEA 连接数据库的实现方法,包括了IDEA 连接数据库的实现方法的使用技巧和注意事项,需要的朋友参考一下 【1】添加 database 到右侧栏 (1.1)先看右侧栏有【database】图标没,从我这里可以看到是没有的; (1.2)如图选择【View】-【Tool Windows】-【Database】 (1.3) 如图所示,右侧栏就可以看见【Database】一栏了。 【2】

  • 本文向大家介绍PowerShell连接SQL SERVER数据库进行操作的实现代码,包括了PowerShell连接SQL SERVER数据库进行操作的实现代码的使用技巧和注意事项,需要的朋友参考一下 核心代码 下面是来自微软的官方帮助文档:Windows PowerShell:使用 PowerShell 处理数据库 您可以使用一些 Windows PowerShell 代码来配置您的数据库,以存储