.NET3

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

  #region 数据库增删改方法
        /// <summary>
        /// 数据库执行增删改
        /// </summary>
        /// <param name="sql">用户编写的sql语句</param>
        /// <returns>执行后返回给用户的处理结果</returns>
        public static int ExecuteNonQuery(string sql, SqlParameter[] sqlParameters)
        {
            //1:创建数据库连接
            SqlConnection connection = new SqlConnection(connectionStr);
            try
            {
                //2;打开数据库连接
                connection.Open();
                //第三步变成直接从外面传一个string变量
                //4:生成Command对象
                SqlCommand comm = new SqlCommand(sql, connection);
                if (sqlParameters != null)
                {
                    comm.Parameters.AddRange(sqlParameters);
                }
                //5:执行SQL语句
                return comm.ExecuteNonQuery();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                //无论数据库操作成功或者失败,都要关闭数据库
                //7;关闭数据库连接
                connection.Close();
            }
        }
        #endregion

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

INSERT INTO 表名
           (列名)
     VALUES
           (值)
GO

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

SELECT * FROM 表名

4:Ajax的提交格式?

 $.ajax({
            type: "post",
            url: "/Login/IsCoreTrue",
            data: { SendPhone: $("#username").val(), Core: $(this).val() },
            success: function (data) {
                if (data.Success) {
                   alert("成功");
                } else {
                   alert("失败");                   
                }
            }
        })

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

#region 数据库返回所有查询的数据
        /// <summary>
        /// 数据库返回一张表的方法
        /// </summary>
        /// <param name="sql">用户编写的sql语句</param>
        /// <returns>执行后返回给用户的处理结果</returns>
        public static SqlDataReader ExecuteReader(string sql, SqlParameter[] sqlParameters)
        {
            //1:创建数据库连接
            SqlConnection connection = new SqlConnection(connectionStr);
            try
            {
                //2;打开数据库连接
                connection.Open();
                //第三步变成直接从外面传一个string变量
                //4:生成Command对象
                SqlCommand comm = new SqlCommand(sql, connection);
                if (sqlParameters != null)
                {
                    comm.Parameters.AddRange(sqlParameters);
                }
                //5:执行SQL语句
                return comm.ExecuteReader(CommandBehavior.CloseConnection);
            }
            catch (Exception)
            {
                throw;
            }
        }
        #endregion

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

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

②NuGit包中添加对应的包

③封装相对应的属性

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

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

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

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

#region 返回DataSet
        /// <summary>
        /// 返回DataSet对象
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="sqlParameters"></param>
        /// <returns></returns>
        public static DataSet Query(string sql, SqlParameter[] sqlParameters)
        {
            DataSet ds = new DataSet();
            //1:创建数据库连接
            SqlConnection connection = new SqlConnection(connectionStr);
            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(ds);
            }
            catch
            {
                return ds;
            }
            return ds;
        }
        #endregion

9:数据集如何转换成List<T> [知识点:反射]

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;
        }

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

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

Session:会话 

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

11: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");

12:什么是路由?如何配置路由?

路由: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 }
            );
        }

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

①ActionResult-->JsonResult

②方法参数改为对象

③return View()-->return Json()

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

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

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

去除Form表单action属性提交

16: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;
        }


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值