C#MVC帮助方法

 

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

 /// <summary>
        /// 增,删,改 (除查询)数据方法
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public static int ExecutNonQuery(string sql, SqlParameter[] parameter)
        {
            //创建数据库连接
            SqlConnection connection = new SqlConnection(connString);
            try
            {
                //打开数据库连接
                connection.Open();

                //创建command对象
                SqlCommand comm = new SqlCommand(sql, connection);
                comm.Parameters.AddRange(parameter);
                //返回结果
                return comm.ExecuteNonQuery();

            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                //关闭数据库连接
                connection.Close();
            }
        }

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

insert into 表名(列名) values(值)

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

/// <summary>
        /// 返回多条记录
        /// </summary>
        /// <param name="sql">接受需要查询的值</param>
        /// <returns>返回出去值</returns>
        public static SqlDataReader ExecutReader(string sql)
        {
            //创建数据库连接
            SqlConnection connection = new SqlConnection(connString);
            try
            {
                //打开数据库连接
                connection.Open();

                //创建command对象
                SqlCommand comm = new SqlCommand(sql, connection);
                //返回结果
                return comm.ExecuteReader(CommandBehavior.CloseConnection);

            }
            catch (Exception)
            {
                throw;
            }

        }

12:Ajax的提交格式?

 $.ajax({
                url: "/Login/AddAdminInfo",//路径
                type: "post",//提交类型
                data: data,//返回对象
                success: function (json) {
                    
                }
            })

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

        /// <summary>
        /// 反回单个
        /// </summary>
        /// <param name="sql">接受用户输入的值</param>
        /// <returns>返回</returns>
        public static object ExecutScalar(string sql, SqlParameter[] parameter)
        {
            //创建数据库连接
            SqlConnection connection = new SqlConnection(connString);
            try
            {
                //打开数据库连接
                connection.Open();

                //创建command对象
                SqlCommand comm = new SqlCommand(sql, connection);
                comm.Parameters.AddRange(parameter);
                //返回结果
                return comm.ExecuteScalar();

            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                //关闭数据库连接
                connection.Close();
            }
        }

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

引用右键点击管理UnGet程序包添加

15:第三方的项目Remote

发送腾出云短信

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


先引用using System.Configuration; 然后ConfigurationManager.AppSettings["appid"]这样获取,如果要强转类型就转

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

 /// <summary>
        /// 数据集
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="parameter"></param>
        /// <returns></returns>
        public static DataSet GetDateQuery(string sql, SqlParameter[] parameter)
        {
            //创建数据库连接
            SqlConnection connection = new SqlConnection(connString);
            DataSet set = new DataSet();
            try
            {
                //打开数据库连接
                connection.Open();
                //创建command对象
                SqlCommand comm = new SqlCommand(sql, connection);
                if (parameter != null)
                {
                    comm.Parameters.AddRange(parameter);
                }
                
                SqlDataAdapter sda = new SqlDataAdapter(comm);//创建数据适配器
                sda.Fill(set);
                //返回结果
                return set;

            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                //关闭数据库连接
                connection.Close();
            }
        }

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

 /// <summary>
        /// DataSet转为List
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="connStr"></param>
        /// <returns></returns>
        public static List<T> DataSetToList<T>(DataTable dt)
        {
            List<T> list = new List<T>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                T t = Activator.CreateInstance<T>();
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    PropertyInfo[] info = t.GetType().GetProperties();
                    foreach (PropertyInfo item in info)
                    {
                        if (dt.Columns[j].ColumnName == item.Name)
                        {
                            if (dt.Rows[i][j] != DBNull.Value)
                            {
                                item.SetValue(t, dt.Rows[i][j]);
                            }
                            else
                            {
                                item.SetValue(t, null);
                            }
                        }
                    }
                }
             list.Add(t);
            }
            return list; 
        }

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

Cookie存储在客户端的   Session存储在每次会话种关闭就没有 

20:Cookie和Session的使用?


//使用cookie
                    HttpCookie cookie = new HttpCookie("KEY");
                    cookie["KEY"] = "值";
                    System.Web.HttpContext.Current.Response.Cookies.Add(cookie);
//使用Session
                    HttpContext.Session["KEY"] = VALUE;

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

 在defaults: new { controller = "控制器", action = "方法", id = UrlParameter.Optional } 

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

把方法值改为JsonResult 
return Json(要返回的对象)

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

success: function (json) {
                    if (json.Success) {
                        alert("添加成功")
                    } else {
                        alert("失败")
                    }
                }

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

可以删除Form可以把submit改为button

25:Sql语句参数化处理。 SqlParameter[] sqls =
            {
                new SqlParameter
                {
                    DbType = DbType.String,
                    ParameterName="@realname",
                    Value="刘长春经"
                },

}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值