C# 基础知识扩充6 工具类增删改查和数据集的方法 ,Ajax有关内容 ,引入第三方类库的方法,WebConfig配置与读取, 数据集转换List<T>有关反射,sql语句参数化处理(防sql注入)

一:工具类如何写一个增删改的方法?

public static int ExecuteNonQuery(string sql,SqlParameter[] sqlParameter)
        {
            //1:创建数据库连接
            SqlConnection connection = new SqlConnection(connectionStr);
            try
            {
                //2;打开数据库连接
                connection.Open();
                //第三步变成直接从外面传一个string变量
                //3:生成Command对象
                SqlCommand comm = new SqlCommand(sql, connection);
                comm.Parameters.AddRange(sqlParameter);
                
                //4:执行SQL语句
                return comm.ExecuteNonQuery();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                //无论数据库操作成功或者失败,都要关闭数据库
                //5;关闭数据库连接
                connection.Close();
            }
        }

返回一个int 如果大于0则返回true 如果小于0则返回false

二:如何写向表里面插入一条记录的sql?

注意列名有多个注意用逗号做分隔

insert into 表名(列名1,列名2) values(值1,值2);

三:怎么查询一张表的记录?

注:*代表全部

select * from 表名

四:Ajax的提交格式?

 $.ajax({
            type: "post",
            url: "/控制器名/控制器中的方法名",
            data: { SendPhone: $("#username").val(), Core: $(this).val() },
            success: function (data) {
                if (data.Success) {
                   alert("成功");
                } else {
                   alert("失败");                   
                }
            }
        })

五:工具类如何写一个查询的方法?

public static object ExecuteScalar(string sql,SqlParameter[] sqlParameters)
        {
            //1:创建数据库连接
            SqlConnection connection = new SqlConnection(connectionStr);
            try
            {
                //2;打开数据库连接
                connection.Open();
                //第三步变成直接从外面传一个string变量
                //3:生成Command对象
				if (sqlParameters!=null)
                {
                    comm.Parameters.AddRange(sqlParameters);
                }
                SqlCommand comm = new SqlCommand(sql, connection);
                
                //4:执行SQL语句
                return comm.ExecuteScalar();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                //无论数据库操作成功或者失败,都要关闭数据库
                //5;关闭数据库连接
                connection.Close();
            }
        } 

六:如何引入第三方类库?

第一步,创建项目Remote,加入第三方提供的类

第二步,NuGit包中添加对应的包

第三步,封装相对应的属性

七:WebConfig如何配置变量的值? 后台如何读取?

放在Web项目下的Web.config中的appSettings中

通过ConfigurationManager.AppSettings[“AppKey”]进行读取

八:工具类如何写一个数据集的方法?

public static DataSet Query(string sql,SqlParameter[] sqlParameters) 
        {
            //1:创建数据库连接
            SqlConnection connection = new SqlConnection(connectionStr);
            DataSet dataSet = new DataSet();
            try
            {
                //2;打开数据库连接
                connection.Open();
                //第三步变成直接从外面传一个string变量
                //4:生成Command对象
                SqlCommand comm = new SqlCommand(sql, connection);
                if (sqlParameters!=null)
                {
                    comm.Parameters.AddRange(sqlParameters);
                }
                SqlDataAdapter sda = new SqlDataAdapter(comm);
                sda.Fill(dataSet);
                
                //5:执行SQL语句
                return dataSet;
            }
            catch (Exception)
            {
                throw;
            }
        }

九:数据集如何转换成List [知识点:反射]

public static List<T> DataTableToList<T>(DataTable table)
        {
            List<T> list = new List<T>();            
            //遍历表每一行
            for (int i = 0; i < table.Rows.Count; i++)
            {
                T model = Activator.CreateInstance<T>();
                PropertyInfo[] properties= typeof(T).GetProperties();
                //遍历表每一列
                for (int j = 0; j < table.Columns.Count; j++)
                {
                    //遍历实体中每一个属性
                    foreach (PropertyInfo item in properties)
                    {
                        //列名和属性名是否一致
                        if (table.Columns[j].ColumnName== item.Name)
                        {
                            //列名值不为空
                            if (table.Rows[i][j] != DBNull.Value)
                            {
                                //赋值
                                properties[j].SetValue(model, table.Rows[i][j]);
                            }
                            else
                            {
                                properties[j].SetValue(model, null);
                            }
                        }
 
                    }
                }
                list.Add(model);
            }
            return list;
        }

十:什么Cookie?什么是Session? 两者有什么区别?

Cookie:网页浏览器用来保存用户信息的文件

Session:会话

Session是存储在服务器端的,Cookie是存储在客户端的

十一:Cookie和Session的使用?

Session用法

//存
Session["user"] = "majcms";
//取
String username = Session["user"].ToString();

Cookie用法

//传值:
HttpCookie httpCookie = System.Web.HttpContext.Current.Response.Cookies.Add("USERINFO_USERNAME");            
 
 //得到cookie值
HttpCookie httpCookie = System.Web.HttpContext.Current.Request.Cookies.Get("USERINFO_USERNAME");

十二:什么是路由?如何配置路由?

路由:URL的访问规则。

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
 
            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }

十三:控制器的方法如何返回一个Json对象

第一步 ActionResult–>JsonResult

第二步 方法参数改为对象

第三步 return View()–>return Json()

十四:Ajax如何接受Json对象并做判断

    success: function (data) {
                            if (data.Success) {
                                alert("注册成功");                                
                                window.location.href = "../Login/Login";
                            } else {
                                alert("注册失败");                            
                                window.location.href = "../Login/Register";
                            }
                        }

十五:Ajax提交,Form表单需要注意什么问题

去除Form表单action属性提交

十六:Sql语句参数化处理。

注 SendPhone=@Phone 中的@可以用来防止sql注入名字可以自定义

public bool RepeatCore(string phone)
        {
            string sql = $"select COUNT(1) from SMSInfo where SendPhone=@Phone and 				EndTime>=GETDATE();";
            SqlParameter[] sqlParameter = {
                new SqlParameter(){
                    DbType=System.Data.DbType.String,
                    ParameterName="@Phone",
                    Value=phone
                }
            };
            return Convert.ToInt32(DBHelper.ExecuteScalar(sql, sqlParameter)) > 0;
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值