• SQLHelper的简单应用,高手绕道,写出最近用的一个类,仅供初学者参考


         为了更好的表达与应用,方便初学者的学习,在此我写出四个一般处理程序,分别实现增、删、改、查,一通百通。

    增:

    add.ashx:

    using System.Web;
    using System.Data.SqlClient;

    namespace test_SQLHelper_
    {
        /// <summary>
        /// Add 的摘要说明
        /// </summary>
        public class Add : IHttpHandler
        {

            public void ProcessRequest(HttpContext context)
            {
                context.Response.ContentType = "text/plain";
                string examsubject=context.Request["examsubject"];
                string languageclass=context.Request["languageclass"];
                SqlParameter[] para={
                new SqlParameter("@ExamSubject",examsubject),
                new SqlParameter("@LanguageClass",languageclass)
                };
               SQLHelper.ExecuteNonQuery(SQLHelper.ConnectionStrings,System.Data.CommandType.Text,"insert into T_ExamCalendar (ExamSubject,LanguageClass) values(@ExamSubject,@LanguageClass)",para);
            }

            public bool IsReusable
            {
                get
                {
                    return false;
                }
            }
        }
    }

    删:

    delete.ashx:

    using System.Web;
    using System.Data.SqlClient;

    namespace test_SQLHelper_
    {
        /// <summary>
        /// delete 的摘要说明
        /// </summary>
        public class delete : IHttpHandler
        {

            public void ProcessRequest(HttpContext context)
            {
                context.Response.ContentType = "text/plain";
                string examsubject = context.Request["examsubject"];
                SqlParameter[] pare = {
                                      new SqlParameter ("@ExamSubject",examsubject)
                                      };
                SQLHelper.ExecuteNonQuery(SQLHelper.ConnectionStrings,System.Data.CommandType.Text,"delete from T_ExamCalendar where ExamSubject=@ExamSubject",pare);
            }

            public bool IsReusable
            {
                get
                {
                    return false;
                }
            }
        }
    }

    改:

    update.ashx:

    using System.Web;
    using System.Data.SqlClient;

    namespace test_SQLHelper_
    {
        /// <summary>
        /// update 的摘要说明
        /// </summary>
        public class update : IHttpHandler
        {

            public void ProcessRequest(HttpContext context)
            {
                context.Response.ContentType = "text/plain";
                string examsubject = context.Request["examsubject"];
                string examaccuratetime = context.Request["examaccuratetime"];
                SqlParameter[] para = {
                                    new SqlParameter ("@ExamSubject",examsubject),
                                    new SqlParameter("@ExamAccurateTime",examaccuratetime)
                                    };
                SQLHelper.ExecuteNonQuery(SQLHelper.ConnectionStrings,System.Data.CommandType.Text,"update T_ExamCalendar set ExamAccurateTime=@ExamAccurateTime where ExamSubject=@ExamSubject",para);
            }

            public bool IsReusable
            {
                get
                {
                    return false;
                }
            }
        }
    }

    查:

    check.ashx:

    using System.Collections.Generic;
    using System.Web;
    using System.Data;
    using System.Web.Script.Serialization;

    namespace test_SQLHelper_
    {
        /// <summary>
        /// check 的摘要说明
        /// </summary>
        public class check : IHttpHandler
        {

            public void ProcessRequest(HttpContext context)
            {
                context.Response.ContentType = "text/plain";
                DataTable dt = new DataTable();
                dt = SQLHelper.ExecuteDataTable(SQLHelper.ConnectionStrings, System.Data.CommandType.Text, "select * from T_ExamCalendar");
                List<Dictionary<string, string>> List = new List<Dictionary<string, string>>();
                for (int i = 0; i < dt.Rows.Count;i++ ) {
                    var Dic = new Dictionary<string, string> {
                    {"ExamSubject",dt.Rows[i]["ExamSubject"].ToString()},
                    {"LanguageClass",dt.Rows[i]["LanguageClass"].ToString()}
                    };
                    List.Add(Dic);
                }
                JavaScriptSerializer jss = new JavaScriptSerializer();
                context.Response.Write(jss.Serialize(List));
            }

            public bool IsReusable
            {
                get
                {
                    return false;
                }
            }
        }
    }

          使用的连接字符串为:(写成SQLHelper的一个字段)

    public static readonly string ConnectionStrings = ConfigurationManager.ConnectionStrings["ConnectionStrings"].ConnectionString;

          于是每次都如此调用,如上:Check.ashx中的(粗体部分为链接字符串,写在web.config中)

    dt = SQLHelper.ExecuteDataTable(SQLHelper.ConnectionStrings, System.Data.CommandType.Text, "select * from T_ExamCalendar");

         web.config中的链接字符串如下:

    <connectionStrings>
      <add name="ConnectionStrings" connectionString="server=CHENSHENGTAI-PC;database=EXAMCALENDAR.MDF;Integrated Security=True;" providerName="System.Data.SqlClient" />
     </connectionStrings>


     

  • 相关阅读:
    js 动态创建HTML元素
    ASP.NET的几种主要文件
    【荐】Asp.net对文件夹和文件的操作类
    201920201学期 20192415 《网络空间安全专业导论》第二周学习总结 第五章
    201920201学期 20192415《网络空间安全专业导论》第一周学习总结 第二章
    201920201学期 20192415 《网络空间安全专业导论》第二周学习总结 第四章
    201920201学期 20192415 《网络空间安全专业导论》第一周学习总结 第三章
    C# 自动生成类
    div左右布局
    EFCodeFirst安装失败(包括EntityFrameWork安装)解决方案
  • 原文地址:https://www.cnblogs.com/chenshengtai/p/2138667.html
Copyright © 2020-2023  润新知