主頁 > 知識庫 > ASP.NET連接sql2008數(shù)據(jù)庫的實(shí)現(xiàn)代碼

ASP.NET連接sql2008數(shù)據(jù)庫的實(shí)現(xiàn)代碼

熱門標(biāo)簽:百度AI接口 客戶服務(wù) 人工智能 國美全國運(yùn)營中心 科大訊飛語音識別系統(tǒng) 電商新玩法 電銷業(yè)務(wù) 網(wǎng)站排名優(yōu)化

利用SqlConnection對象連接sql2000以上版本,并使用SqlCommand對象對數(shù)據(jù)庫進(jìn)行讀取。

SqlCommand類概述:

 用于對sql數(shù)據(jù)庫執(zhí)行sql語句或存儲過程。

 命名空間:System.Data.SqlClient

   程序集: System.Data(在 System.Data.dll中)

SqlCommand類的屬性

1.CommandText

  獲取或設(shè)置要對數(shù)據(jù)源執(zhí)行的Transact—SQL語句或存儲過程。

2. CommandType

獲取或設(shè)置一個值,該值指示如何解釋CommandText屬性,CommandType默認(rèn)為CommandType.Text,表示執(zhí)行sql語句,調(diào)用存儲過程時需設(shè)CommandType.StoredProcedure。3.Connection

  獲取或設(shè)置SqlCommand的實(shí)例使用的SqlConnection。

4.CommandTimeOut

  獲取或設(shè)置在終止執(zhí)行命令的嘗試并生成錯誤之前的等待時間。

 SqlCommand類的方法

1.ExecuteNonQuery:   通過該命令執(zhí)行不要返回值的操作,例如UPDATE,INSERT,DELETE等SQL命令,只是返回執(zhí)行該命令所影響到表的行數(shù)。
2.ExecuteScalar: 可用來執(zhí)行SELECT查詢,但返回的是一個單一的值,用于查詢聚合,例如使用count(), sum(),等函數(shù)的SQL指令。
3.ExecuteReader:  該方法返回一個DataReader對象,內(nèi)容為查詢結(jié)果的內(nèi)容集合。

以下通過SqlConnection連接sql2008,并執(zhí)行數(shù)據(jù)簡單操作的代碼:

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數(shù)據(jù)庫
    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();
  }
}

改為執(zhí)行sql指令后的代碼,實(shí)現(xiàn)同樣效果。

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數(shù)據(jù)庫
    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();
  }
}

運(yùn)行效果:

項(xiàng)目代碼已上傳。

您可能感興趣的文章:
  • asp.net 使用ObjectDataSource控件在ASP.NET中實(shí)現(xiàn)Ajax真分頁
  • 動態(tài)指定任意類型的ObjectDataSource對象的查詢參數(shù)
  • asp.net中將數(shù)據(jù)庫綁定到DataList控件的實(shí)現(xiàn)方法與實(shí)例代碼
  • gridview+objectdatasource+aspnetpager整合實(shí)例
  • ASP.NET web.config中數(shù)據(jù)庫連接字符串connectionStrings節(jié)的配置方法
  • ASP.NET中操作SQL數(shù)據(jù)庫(連接字符串的配置及獲取)
  • ASP.NET連接MySql數(shù)據(jù)庫的2個方法及示例
  • ASP.NET連接數(shù)據(jù)庫并獲取數(shù)據(jù)方法總結(jié)
  • ASP.NET中 ObjectDataSource控件的DataObjectTypeName屬性

標(biāo)簽:南平 棗莊 益陽 廈門 咸寧 攀枝花 POS機(jī) 拉薩

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《ASP.NET連接sql2008數(shù)據(jù)庫的實(shí)現(xiàn)代碼》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266