新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
C#數(shù)據(jù)訪問層的相關(guān)知識
C#數(shù)據(jù)訪問層1.查詢數(shù)據(jù)庫中的數(shù)據(jù),返回一個datatable

站在用戶的角度思考問題,與客戶深入溝通,找到黃巖網(wǎng)站設(shè)計與黃巖網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗,讓設(shè)計與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:成都做網(wǎng)站、網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣、域名注冊、虛擬空間、企業(yè)郵箱。業(yè)務(wù)覆蓋黃巖地區(qū)。
C#數(shù)據(jù)訪問層2.執(zhí)行一條SQL語句已重載
- using System;
- using System.Data;
- using NUnit.Framework;
- using CodeFilemanger.Project;
- using System.Data.SqlClient;
- using System.Configuration;
- namespace OperatorDB
- {
- ///
- /// Class1 的摘要說明。
- ///
- [NUnit.Framework.TestFixture]
- public class OperatorDB
- {
- private static string strCon = ConfigurationSettings.AppSettings["ConnectionString"] ;
- private int ModuleId = 1;
- public static string ConnectionString
- {
- get
- {
- return strCon;
- }
- set
- {
- strCon = value;
- }
- }
- #region "初始化"
- [NUnit.Framework.TestFixtureSetUp]
- public void Register_Module()
- {
- string ModuleName = "OperatorDB";
- string ModuleAuthor = "MYM";
- string ModuleDescribe = "數(shù)據(jù)訪問模塊";
- string CreateDatetime = "2003-5-30";
- ModuleId = Project.InsertModule( ModuleName, ModuleAuthor, ModuleDescribe, CreateDatetime) ;
- }
- [Test]
- public void Register_Method_SelectData()
- {
- string MethodName = "SelectData";
- string MethodAuthor = "MYM";
- string MethodCreateDateTime = "2005-3-30";
- string MethodParaMeters ="ParaMeters(string SqlCommandText, System.Data.DataTable Dt, bool RowsClearr)";
- string MethodReturn = "bool";
- string MethodCall = "" ;
- string MethodDescribe = "查詢數(shù)據(jù)庫中的數(shù)據(jù),返回一個datatable";
- Project.InsertMethod(MethodName,MethodAuthor,MethodCreateDateTime,MethodParaMeters,MethodReturn,MethodCall,MethodDescribe,ModuleId);
- }
- [Test]
- public void Register_Method_ExecuteSql()
- {
- string MethodName = "ExecuteSql";
- string MethodAuthor = "MYM";
- string MethodCreateDateTime = "2005-3-30";
- string MethodParaMeters ="ParaMeters(string SqlCommandText)";
- string MethodReturn = "int";
- string MethodCall = "" ;
- string MethodDescribe = "執(zhí)行一條SQL語句";
- Project.InsertMethod(MethodName,MethodAuthor,MethodCreateDateTime,MethodParaMeters,MethodReturn,MethodCall,MethodDescribe,ModuleId);
- }
- [Test]
- public void Register_Method_SerialNumber()
- {
- string MethodName = "SerialNumber";
- string MethodAuthor = "MYM";
- string MethodCreateDateTime = "2005-3-30";
- string MethodParaMeters ="ParaMeters(int index, System.Data.DataTable dt)";
- string MethodReturn = "void";
- string MethodCall = "" ;
- string MethodDescribe = "給表的指定列添加序號";
- Project.InsertMethod(MethodName,MethodAuthor,MethodCreateDateTime,MethodParaMeters,MethodReturn,MethodCall,MethodDescribe,ModuleId);
- }
- #endregion
- public static bool SelectData(string SqlCommandText, System.Data.DataTable Dt, bool RowsClearr)
- {
- strCon = ConfigurationSettings.AppSettings["ConnectionString"];
- bool ret = true;
- if (SqlCommandText != "")
- {
- if (RowsClearr)
- {
- if (Dt.Rows.Count > 0)
- {
- Dt.Rows.Clear();
- }
- }
- SqlConnection cn = new SqlConnection(strCon);
- SqlDataAdapter da = new SqlDataAdapter(SqlCommandText, cn);
- try
- {
- cn.Open();
- da.Fill(Dt);
- }
- catch (System.Exception ex)
- {
- ExceptionHand exc = new ExceptionHand(ex);
- exc.DisplayErrorMessager("OperatorDB","SelectData",SqlCommandText);
- ret = false;
- }
- if (cn.State == ConnectionState.Open)
- {
- cn.Close();
- }
- da.Dispose();
- }
- else
- {
- ret = false;
- }
- return ret;
- }
- public static int ExecuteSql(string SqlCommandText)
- {
- int ID = 0;
- strCon = ConfigurationSettings.AppSettings["ConnectionString"];
- if (SqlCommandText != "")
- {
- SqlConnection cn = new SqlConnection(strCon);
- SqlCommand cm = new SqlCommand(SqlCommandText, cn);
- try
- {
- cn.Open();
- ID = Convert.ToInt32(cm.ExecuteScalar());
- }
- catch (System.Exception ex)
- {
- cn.Close();
- ExceptionHand exc = new ExceptionHand(ex);
- exc.DisplayErrorMessager("OperatorDB","ExecuteSql",SqlCommandText);
- ID = -1;
- }
- if (cn.State == ConnectionState.Open)
- {
- cn.Close();
- }
- cm.Dispose();
- }
- return ID;
- }
- public static int ExecuteSql(SqlCommand Cm)
- {
- int ID = 0;
- strCon = ConfigurationSettings.AppSettings["ConnectionString"];
- SqlConnection cn = new SqlConnection(strCon);
- try
- {
- cn.Open();
- Cm.Connection = cn;
- ID = Convert.ToInt32(Cm.ExecuteScalar());
- }
- catch (System.Exception ex)
- {
- cn.Close();
- ExceptionHand exc = new ExceptionHand(ex);
- exc.DisplayErrorMessager("OperatorDB","ExecuteSql",Cm.CommandText);
- ID = -1;
- }
- if (cn.State == ConnectionState.Open)
- {
- cn.Close();
- }
- Cm.Dispose();
- return ID;
- }
- public static void SerialNumber(int index, System.Data.DataTable dt)
- {
- for (int i = 0; i <= dt.Rows.Count - 1; i++)
- {
- dt.Rows[i][index] = i + 1;
- }
- }
- public static void SetSqlCommandValues(SqlCommand Com,DataTable Dt,int Index,int StartIndex)
- {
- int i;
- for (i=StartIndex;i{
- Com.Parameters.Add("@" + Dt.Columns[i].ColumnName,Dt.Rows[Index][i]);
- }
- }
- }
- }
C#數(shù)據(jù)訪問層的相關(guān)知識就介紹到這里。
分享標(biāo)題:C#數(shù)據(jù)訪問層的相關(guān)知識
文章網(wǎng)址:http://www.dlmjj.cn/article/dppgige.html


咨詢
建站咨詢
