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#数据访问层的相关知识
浏览路径:http://www.mswzjz.cn/qtweb/news1/252001.html
攀枝花网站建设、攀枝花网站运维推广公司-贝锐智能,是专注品牌与效果的网络营销公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 贝锐智能