What is the relevant knowledge of C# data access layer
This article is to share with you about the relevant knowledge of the C# data access layer. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
C # data access layer 1. Query the data in the database and return a datatable
C # data access layer 2. Execution of a SQL statement has been overloaded
Summary description of 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 "initialization" [NUnit.Framework.TestFixtureSetUp] public void Register_Module () {string ModuleName = "OperatorDB"; string ModuleAuthor = "MYM"; string ModuleDescribe = "data access module" 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 = "query data in the database and return a 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 = "execute a SQL statement"; 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 = "add ordinal to the specified column of the table"; 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)