189 8069 5689

如何在ASP.NET项目中连接sql2008数据库-创新互联

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

创新互联公司为客户提供专业的成都网站设计、成都做网站、程序、域名、空间一条龙服务,提供基于WEB的系统开发. 服务项目涵盖了网页设计、网站程序开发、WEB系统开发、微信二次开发、手机网站开发等网站方面业务。

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("");
    Response.Write("");
    for (int i = 0; i < myReader.FieldCount; i++)
      Response.Write("" + myReader.GetName(i) + "");
    Response.Write("");

    while (myReader.Read())
    {
      Response.Write("");
      for (int i = 0; i < myReader.FieldCount; i++)
        Response.Write("" + myReader[i].ToString() + "");
      Response.Write("");
    }
    Response.Write("");

    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("");
    Response.Write("");
    for (int i = 0; i < myReader.FieldCount; i++)
      Response.Write("" + myReader.GetName(i) + "");
    Response.Write("");

    while (myReader.Read())
    {
      Response.Write("");
      for (int i = 0; i < myReader.FieldCount; i++)
        Response.Write("" + myReader[i].ToString() + "");
      Response.Write("");
    }
    Response.Write("");

    myReader.Close();
    myConnection.Close();
  }
}

上述就是小编为大家分享的如何在ASP.NET项目中连接sql2008数据库了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注创新互联行业资讯频道。


网页名称:如何在ASP.NET项目中连接sql2008数据库-创新互联
文章网址:http://cdxtjz.cn/article/djjjgj.html

其他资讯