01:工具类如何写一个增删改的方法
public static int GetNum(string sql, SqlParameter[] parameters)
{
string str = "server = localhost;database=MIShop;uid=sa;pwd=sa;";
SqlConnection conn = null;
int num = 0;
try
{
conn = new SqlConnection(str );
conn.Open();
SqlCommand comm = new SqlCommand(sql, Conn);
if (parameters != null)
{
comm.Parameters.AddRange(parameters);
}
num = comm.ExecuteNonQuery();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
return num;
}
02:如何写向表里面插入一条记录的sql
insert into 表名(列1,列2...)values(值1,值2...)
03:怎么查询一张表的记录
select * from 表面
04:Ajax的提交格式
url: "/Login/AddAdminInfo",
05:工具类如何写一个查询的方法
public static SqlDataReader GetReader(string sql, SqlParameter[] parameters)
{
SqlDataReader reader = null;
try
{
SqlCommand comm = new SqlCommand(sql, Conn);
if (parameters != null)
{
comm.Parameters.AddRange(parameters);
}
conn.Open();
reader = comm.ExecuteReader();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
return reader;
}
06:如何引入第三方类库
管理NuGet包,浏览安装
07:第三方的项目Remote
08:WebConfig如何配置变量的值? 后台如何读取?
<appSettings>
<add key="webpages" value="3.0.0.0"/>
</appSettings>
string str = ConfigurationManager.AppSettings["webpages"];
09:工具类如何写一个数据集的方法
public static DataSet GetDataSet(string sql, SqlParameter[] parameters)
{
string str = "server = localhost;database=MIShop;uid=sa;pwd=sa;";
SqlConnection conn = new SqlConnection(str );
conn.Open();
DataSet dataSet = new DataSet();
//预处理
SqlCommand comm = new SqlCommand(sql, Conn);
if (parameters != null)
{
comm.Parameters.AddRange(parameters);
}
//适配器
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(comm);
//装载数据集
sqlDataAdapter.Fill(dataSet);
return dataSet;
}
10:数据集如何转换成List [知识点:反射]
public static List<T> DataTableTolist<T>(DataTable dt)
{
List<T> result = new List<T>();
for (int i = 0;i<dt.Rows.Count;i++)
{
//将T反射
T type = Activator.CreateInstance<T>();
for (int j = 0;j<dt.Columns.Count;j++)
{
//获取当前type的所有属性集合
PropertyInfo[] propertyInfos = type.GetType().GetProperties();
//遍历属性
foreach (PropertyInfo info in propertyInfos)
{
//如果当前列名等于当前属性名称
if (dt.Columns[j].ColumnName == info.Name)
{
//给当前的属性赋值
if (dt.Rows[i][j] != DBNull.Value)
{
info.SetValue(type, dt.Rows[i][j]);
}
else
{
info.SetValue(type, null);
}
}
}
}
result.Add(type);
}
return result;
}
11:什么Cookie?什么是Session? 两者有什么区别
- Cookie 保存在浏览器
- Session 保存在服务器
12:Cookie和Session的使用
- 在登陆成功,跳转页面之前存入Cookie:
HttpCookie cookie = new HttpCookie("123"); //创建Cookie ("随便取名")
cookie["123"] = user.Name;
cookie.Expires = DateTime.Now.AddDays(1); //设置Cookie时间为1天
System.Web.HttpContext.Current.Response.Cookies.Add(cookie); //添加Cookie到浏览器
Response.Redirect("/Home/Index"); //跳转页面
- 加载Cookie
HttpCookie cookie = System.Web.HttpContext.Current.Request.Cookies.Get("123");
var username = cookie["123"];
Session
- 项目下 Models 文件夹里创建类 UserContext.cs (上下文类)
public class UserContext
{
private const string str = "KEY"; //随便取名
public HttpSessionState httpSessionState = HttpContext.Current.Session;
public static UserContext context = new UserContext(); //静态 new 自己
public User user //实体层用户信息类
{
get
{
return this.httpSessionState[str] as User;
}
set
{
this.httpSessionState[str] = value;
}
}
}
- 在登陆成功,跳转页面之前存入
UserContext.context.user = user; //类名.静态属性.实体类对象 = 刚登陆的实体对象;
- 首页页面读取:
@using Frist.Models;
@{
var username = UserContext.context.user?.Name;
}
- 注销Session
UserContext.context.user = null;
13:什么是路由?如何配置路由
路由就是一个路径的解析,根据客户端提交的路径,将请求解析到相应的控制器上;
从 URL 找到处理这个 URL 的类和函数。
配置路由时
项目下的Contorls文件夹下的ValuesController会出现一些方法,但webapi请求语法并不是那么的好用,所以我们先修改路由规则,使其更符合我们平常使用MVC的设计习惯:
(1) 将routeTemplate: “api/{controller}/{id}”,修改为routeTemplate: “api/{controller}/{action}/{id}”,
(2) 之后你就发现我们也需要传方法名称才可以到指定的方法
14:控制器的方法如何返回一个Json对象
return Json(结果)
15:Ajax如何接受Json对象并做判断
success: function (json) {
if (json.Success ) {
alert("成功");
window.location.href = "/Login/Loginn";
} else {
alert("失败");
}
}
16:Ajax提交,Form表单需要注意什么问题
- 删除表单提交地址
17:Sql语句参数化处理。
SqlParameter[] parameters = {
new SqlParameter() {
DbType = System.Data.DbType.String,
ParameterName = "@Name",
Value = user.Name
}
};