一個資料操作底層類

來源:互聯網
上載者:User
資料 我對這個類不是很滿意,如果你有改進意見請聯絡我,不勝感激.

主要將對資料庫的操作簡單封裝了一下

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Configuration;
using System.Data.SqlClient;

namespace Addr.Admin.ExecuteSqlCommand
{
?///
?/// ExecuteCommand 的摘要說明:資料處理層。
?///
?public class ExecuteCommand
?{
??string cnStr;
??string errorMessage="";
??bool successful=true;
??//建構函式將連接字串賦給變數
??public ExecuteCommand()
??{
???//
???// TODO: 在此處添加建構函式邏輯
???//
???cnStr=ConfigurationSettings.AppSettings["cn"];
??}
??//枚舉執行sqlcommand的類型
??public enum cmdType
??{
???ExecuteNonQuery=1,
???ExecuteScalar,
???ExecuteReader,
???ExecuteDataSet,
???ExecuteID
??};
??//返回是否執行成功的屬性
??public bool rlt
??{
???get
???{
????return successful;
???}
??}
??//返回錯誤資訊
??public string msg
??{
???get
???{
????return errorMessage;
???}
??}
??//執行命令的函數
??public object Executing(SqlCommand cm,cmdType type)
??{
???SqlConnection cn;
???try
???{
????cn=new SqlConnection(cnStr);
????cn.Open();
????cm.Connection=cn;

????switch(type)
????{
?????case cmdType.ExecuteNonQuery:
??????try
??????{
???????cm.ExecuteNonQuery();
???????cn.Close();
??????}
??????catch(Exception ex)
??????{
???????errorMessage="Error executing 'ExecuteNonQuery'."+ex.Message;
???????successful=false;
??????}
??????break;
?????case cmdType.ExecuteReader:
??????try
??????{
???????SqlDataAdapter da=new SqlDataAdapter(cm.CommandText,cn);
???????DataSet ds=new DataSet();
???????da.Fill(ds,"First");
???????da.Dispose();
???????cn.Close();
???????return ds.Tables["First"];
??????}
??????catch
??????{
???????errorMessage="Error executing 'ExecuteReader'.";
???????successful=false;
??????}
??????break;
?????case cmdType.ExecuteScalar:
??????try
??????{
???????int result=(int)cm.ExecuteScalar();
???????cn.Close();
???????return result;
??????}
??????catch
??????{
???????errorMessage="Error executing 'ExecuteScalar'.'";
???????successful=false;
??????}
??????break;
?????case cmdType.ExecuteDataSet:
??????try
??????{
???????SqlDataAdapter da=new SqlDataAdapter(cm.CommandText,cn);
???????DataSet ds=new DataSet();
???????da.Fill(ds);
???????cn.Close();
???????return ds;
??????}
??????catch
??????{
???????errorMessage="Error executing 'ExecuteDataSet'.";
???????successful=false;
??????}
??????break;
?????case cmdType.ExecuteID:
??????try
??????{
???????string rst="";
???????SqlDataReader dr=cm.ExecuteReader();
???????if(dr.Read())
???????{
????????rst=dr["ID"].ToString();
???????}
???????dr.Close();
???????cn.Close();
???????return rst;
??????}
??????catch
??????{
???????errorMessage="Error executing 'ExecuteID'.";
???????successful=false;
??????}
??????break;
?????default:
??????errorMessage="Error executing unknown sqlcommand.";
??????successful=false;
??????break;
????}
????return successful;
???}
???catch
???{
????successful=false;
????errorMessage="Error opening data connection.";
????return successful;
???}
??}
?}
}





相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

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

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