C#_MVC三层理念


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

public static bool ExecuteQuery(string sql,SqlParameter [] parameters)
{
    SqlConnection  conn=new SqlConnection(ConnString);
    try
    {
        conn.Open();
        
        SqlCommand  comm=new  SqlCommand(sql,conn);
        comm.Paramters.AddRange(parameters);
        return comm.ExecuteQuery()>0;
    }
    catch(Exeption)
    {
        throw;
    }
    finally
    {
        conn.Close();
    }


2:如何写向表里面插入一条记录的sql?
insert 表名(列名) values(值)

3:怎么查询一张表的记录?
select *from 表名

4:Ajax的提交格式?
 $.ajax({
            type: "提交方式",
             url: "提交地址”,
             data:提交数据,
        })

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

public static object ExecutScalar(string sql, SqlParameter[] parameters)
        {
            //创建数据库连接
            SqlConnection connection = new SqlConnection(connString);
            try
            {
                //打开数据库连接
                connection.Open();
 
                //创建command对象
                SqlCommand comm = new SqlCommand(sql, connection);
                comm.Parameters.AddRange(parameters);
                //返回结果
                return comm.ExecuteScalar();
 
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                //关闭数据库连接
                connection.Close();
            }
        }

6:如何引入第三方类库?
首先创建个Remote类库然后在选择引用然后右击选中管理NuGet程序包搜索并下载安装qcloudsms_csharp

7:第三方的项目Remote

using qcloudsms_csharp;
using qcloudsms_csharp.httpclient;
using qcloudsms_csharp.json;
using System;
 
namespace MiShop.Remote
{
    /// <summary>
    /// 腾讯第三方发送验证码帮助类
    /// </summary>
    public class TenXunYunSMS
    {
        //appId
        public int appId;
        //appKey
        public string appKey = "";
        //短信模板ID
        private int tmplateId = 379257;
        //签名内容
        private string smsSign = "7hhhcn";
 
        /// <summary>
        /// 验证码
        /// </summary>
        public int Code { get; set; }
        /// <summary>
        /// 发送验证码
        /// </summary>
        /// <param name="phone"></param>
        /// <returns></returns>
        public void SetSMS(string phone)
        {
            Random random = new Random();
            int code = random.Next(100000, 999999);
            try
            {
                SmsSingleSender ssender = new SmsSingleSender(appId, appKey);
                var result = ssender.sendWithParam("86", phone,
                    tmplateId, new[] { code.ToString() }, smsSign, "", "");  // 签名参数未提供或者为空时,会使用默认签名发送短信
            }
            catch (JSONException ex)
            {
            }
            catch (HTTPException ex)
            {
            }
            catch (Exception ex)
            {
            }
            Code = code;
        }
    }
}


8:WebConfig如何配置变量的值? 后台如何读取?
配置:<add key="名字" value="值" />
读取: string str=ConfigurationManager.AppSettings["名字"];

9:工具类如何写一个数据集的方法?
 public static DataSet ExecutReader(string sql, SqlParameter[] parameters)
        {
            DataSet dataSet = new DataSet();
            //创建数据库连接
            SqlConnection connection = new SqlConnection(connString);
            try
            {
                //打开数据库连接
                connection.Open();
 
                //创建command对象
                SqlCommand comm = new SqlCommand(sql, connection);
                if (parameters != null)
                {
                    comm.Parameters.AddRange(parameters);
                }
                SqlDataAdapter sqlData = new SqlDataAdapter(comm);
                sqlData.Fill(dataSet);
                //返回结果
                return dataSet;
 
            }
            catch (Exception)
            {
                return dataSet;
            }
            finally
            {
                connection.Close();
            }
 
        }

10:数据集如何转换成List<T> [知识点:反射]
   public  static List<T> DataTable<T>(DataTable dt)
        {
            List<T> list = new List<T>();
            for (int i = 0; i <dt.Rows.Count; i++)
            {
                //创建当前对象
                T type = Activator.CreateInstance<T>();
 
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    PropertyInfo[] propertyInfos = type.GetType().GetProperties();
 
                    foreach (PropertyInfo item in propertyInfos)
                    {
                        //如果当前列的名称等于当前属性
                        if (dt.Columns[j].ColumnName==item.Name)
                        {
                            //如果当前列不等于空
                            if (dt.Rows[i][j] != DBNull.Value)
                            {
                                item.SetValue(type, dt.Rows[i][j]);//赋值
                            }
                            else
                            {
                                item.SetValue(type, null);
                            }
                        }
                    }
                }
               list.Add(type);
            }
            return list;
        }

11:什么Cookie?什么是Session? 两者有什么区别?
Cookie是储存在用户本地终端上的数据
Session是会话临时的

12:Cookie和Session的使用?
Cookie可以存储用户登录信息以下次就无须登录
Session是存储用户本次的状态

13:什么是路由?如何配置路由?
public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Login", action = "login", id = UrlParameter.Optional }
            );
        }

14:控制器的方法如何返回一个Json对象
方法返回类型改成JsonResult,return Json(对象名);


15:Ajax如何接受Json对象并做判断
$.ajax({
            url: "",
            data: ,
            type: "",
            success: function (json) {
                if () {
                    
                } else {
                                   
        }
            }
        })

16:Ajax提交,Form表单需要注意什么问题
删除form标签

17:Sql语句参数化处理。
SqlParameter[] sqlcs =
           {
                new SqlParameter()
                {
                    DbType=" ",
                    ParameterName=" ",
                    Value=" ";
                }
         }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值