溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

C#數(shù)據(jù)訪問層的相關(guān)知識有哪些

發(fā)布時間:2021-12-01 14:46:32 來源:億速云 閱讀:162 作者:小新 欄目:編程語言

這篇文章給大家分享的是有關(guān)C#數(shù)據(jù)訪問層的相關(guān)知識有哪些的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

C#數(shù)據(jù)訪問層1.查詢數(shù)據(jù)庫中的數(shù)據(jù),返回一個datatable

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]);  }  }   }  }

感謝各位的閱讀!關(guān)于“C#數(shù)據(jù)訪問層的相關(guān)知識有哪些”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI