好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

主頁 > 知識庫 > ASP.NET連接sql2008數據庫的實現代碼

ASP.NET連接sql2008數據庫的實現代碼

熱門標簽:外呼電銷機器人軟件 河南語音外呼系統公司 t3出行地圖標注怎么做 關于宗地圖標注技術規范 河北網絡回撥外呼系統 400電話辦理最優質 威海電銷 寧夏機器人電銷 400免費電話怎么辦理

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

運行效果:

項目代碼已上傳。

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

標簽:淮北 賀州 固原 廣元 咸寧 樂山 池州 吉林

巨人網絡通訊聲明:本文標題《ASP.NET連接sql2008數據庫的實現代碼》,本文關鍵詞  ASP.NET,連接,sql2008,數據庫,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《ASP.NET連接sql2008數據庫的實現代碼》相關的同類信息!
  • 本頁收集關于ASP.NET連接sql2008數據庫的實現代碼的相關信息資訊供網民參考!
  • 推薦文章