淺談Asp.net多層架構中的變數引用與傳遞

來源:互聯網
上載者:User

最近比較清閑,正好利用這個時間仔細研究了一下Asp.net的多層架構,主要參考的是 Wrox 的一本<.Net WebSite Programming Problem-Design-Solution>,個人覺得這本書寫的不錯。面向有一定.net基礎的開發人員,剛開始看起來可能覺得很難懂,但是仔細研究一下會發現,這本書是一本面向工程應用的優秀參考手冊。

Asp.net的多層架構主要是為瞭解決資料層,邏輯層,展示層等之間的關係。我的做法是這樣的:首先建立一個DataCore的基類。基類裡面封裝了一些低層的資料庫的基本操作,比如說資料庫聯結,調用預存程序等等。在這裡面有一個地方值得注意,通過對一個函數的重載可以實現調用不同功能的預存程序。以下程式碼範例:

protected int RunProcedure(string storedProcName, IDataParameter[] parameters, out int rowsAffected )
{
int result;

Connection.Open();
SqlCommand command = BuildIntCommand( storedProcName, parameters );
rowsAffected = command.ExecuteNonQuery();
result = (int)command.Parameters["ReturnValue"].Value;
Connection.Close();
return result;
}

protected SqlDataReader RunProcedure(string storedProcName, IDataParameter[] parameters )
{
SqlDataReader returnReader;

Connection.Open();
SqlCommand command = BuildQueryCommand( storedProcName, parameters );
command.CommandType = CommandType.StoredProcedure;

returnReader = command.ExecuteReader();
//Connection.Close();
return returnReader;
}

protected DataSet RunProcedure(string storedProcName, IDataParameter[] parameters, string tableName )
{
DataSet dataSet = new DataSet();
Connection.Open();
SqlDataAdapter sqlDA = new SqlDataAdapter();
sqlDA.SelectCommand = BuildQueryCommand( storedProcName, parameters );
sqlDA.Fill( dataSet, tableName );
Connection.Close();

return dataSet;
}

protected void RunProcedure(string storedProcName, IDataParameter[] parameters, DataSet dataSet, string tableName )
{
Connection.Open();
SqlDataAdapter sqlDA = new SqlDataAdapter();
sqlDA.SelectCommand = BuildIntCommand( storedProcName, parameters );
sqlDA.Fill( dataSet, tableName );
Connection.Close();
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.