C#数据访问层的相关知识有哪些
发表于:2025-11-08 作者:千家信息网编辑
千家信息网最后更新 2025年11月08日,这篇文章给大家分享的是有关C#数据访问层的相关知识有哪些的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。C#数据访问层1.查询数据库中的数据,返回一个datatableC#数
千家信息网最后更新 2025年11月08日C#数据访问层的相关知识有哪些
这篇文章给大家分享的是有关C#数据访问层的相关知识有哪些的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
C#数据访问层1.查询数据库中的数据,返回一个datatable
C#数据访问层2.执行一条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 = "数据访问模块"; 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 = "查询数据库中的数据,返回一个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 = "执行一条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#数据访问层的相关知识有哪些"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
数据
知识
C#
内容
数据库
更多
篇文章
语句
查询
不错
实用
序号
摘要
文章
模块
看吧
参考
帮助
有关
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
越南adsl服务器
做软件开发应具备哪些条件
服务器生存模式没有命令方块
京东数据库升级
苹果手机玩游戏服务器爆炸
网络技术起源于
宽城区智能网络技术服务口碑推荐
学校组织开展网络安全自查工作
服务器存在的安全问题
软件开发机试试题
留守儿童网络安全问题
数据库如何统计数量的排名
邯郸正规软件开发报价
洛杉矶网络安全
百单网互联网科技骗局
软件开发工作室装修一千多平
vue请求服务器资源端口不正确
浙江新一代网络技术市场价
以下关于数据库性能监视
江苏网络服务器机柜厂家供应
数据库软件win10
苏州微信小程序软件开发
荣耀大天使服务器开服时间
深圳华强北棋牌软件开发
共筑网络安全防线研讨
国外服务器代理翻墙
广州商城软件开发公司
通信网络技术讲座
java专业编程软件开发
dnf服务器丢失