十年網(wǎng)站開發(fā)經(jīng)驗 + 多家企業(yè)客戶 + 靠譜的建站團隊
量身定制 + 運營維護+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
這期內(nèi)容當中小編將會給大家?guī)碛嘘P如何在ASP.NET項目中連接sql2008數(shù)據(jù)庫,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

SqlCommand類的屬性
1.CommandText
獲取或設置要對數(shù)據(jù)源執(zhí)行的Transact—SQL語句或存儲過程。
2. CommandType
獲取或設置一個值,該值指示如何解釋CommandText屬性,CommandType默認為CommandType.Text,表示執(zhí)行sql語句,調(diào)用存儲過程時需設CommandType.StoredProcedure。3.Connection
獲取或設置SqlCommand的實例使用的SqlConnection。
4.CommandTimeOut
獲取或設置在終止執(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("| " + myReader.GetName(i) + " | "); Response.Write("
| " + myReader[i].ToString() + " | "); Response.Write("
改為執(zhí)行sql指令后的代碼,實現(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("| " + myReader.GetName(i) + " | "); Response.Write("
| " + myReader[i].ToString() + " | "); Response.Write("
上述就是小編為大家分享的如何在ASP.NET項目中連接sql2008數(shù)據(jù)庫了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道。