温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何在ASP.NET项目中连接sql2008数据库

发布时间:2021-02-08 17:08:11 来源:亿速云 阅读:188 作者:Leah 栏目:开发技术

这期内容当中小编将会给大家带来有关如何在ASP.NET项目中连接sql2008数据库,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

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数据库了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI