00:工具类如何写一个增删改的方法?
public static int ExecuteNonQuery(string sql,SqlParameter[] sqlParameter)
{
//创建数据库连接
SqlConnection conn = new SqlConnection(connString);
try
{
//打开数据库连接
conn.Open();
//生成Command对象
SqlCommand comm = new SqlCommand(sql, conn);
comm.Parameters.AddRange(sqlParameter);
//执行sql语句
int num = (int)comm.ExecuteNonQuery();
return num;
}
catch (Exception)
{
throw;
}
finally
{
//关闭数据库连接
conn.Close();
}
}
01:如何写向表里面插入一条记录的sql?
INSERT INTO UsetInfo(UserName,UserPwd,Phone,LoginStat,UserDate,NiChen)
VALUES('xlz','12345','12345678901','False','2019/7/5 15:41:29','xx')
1:怎么查询一张表的记录?
select * from 表名;
2:Ajax的提交格式?
$.ajax({
type: "post",
url: "/Login/SeleUserInfoByNameAndPwd",
data: data,
success: function (opes) {
if (opes.Success) {
alert("登录成功");
window.location.href = "/Home/Index";
} else {
alert("用户名错误或者密码错误");
}
}
});
3:工具类如何写一个查询的方法?
public static SqlDataReader ExecuteReader(string sql)
{
//创建数据库连接
SqlConnection conn = new SqlConnection(connString);
try
{
//打开数据库连接
conn.Open();
//生成Command对象
SqlCommand comm = new SqlCommand(sql, conn);
//执行sql语句
return comm.ExecuteReader(CommandBehavior.CloseConnection);
}
catch (Exception)
{
throw;
}
}
4:如何引入第三方类库?
①创建项目Remote,加入第三方提供的类
②NuGit包中添加对应的包
③封装相对应的属性
5:WebConfig如何配置变量的值? 后台如何读取?
放在Web项目下的Web.config中的appSettings中
通过ConfigurationManager.AppSettings["AppKey"]进行读取
6:工具类如何写一个数据集的方法?
public static DataSet Querw(string sql,SqlParameter[]sp)
{
//创建数据库连接
SqlConnection conn = new SqlConnection(connString);
DataSet ds = new DataSet();
try
{
//打开数据库连接
conn.Open();
//生成Command对象
SqlCommand comm = new SqlCommand(sql, conn);
if (sp!=null)
{
comm.Parameters.AddRange(sp);
}
SqlDataAdapter sda = new SqlDataAdapter(comm);
sda.Fill(ds);
//执行sql语句
return ds;
}
catch (Exception)
{
throw;
}
}
7:数据集如何转换成List<T> [知识点:反射]
public static List<T> DataTableToList<T>(DataTable tableHelper)
{
List<T> rest = new List<T>();
for (int i = 0; i < tableHelper.Rows.Count; i++)
{
T type = Activator.CreateInstance<T>();
for (int j = 0; j < tableHelper.Columns.Count; j++)
{
PropertyInfo[] propertyInfos = type.GetType().GetProperties();
foreach (PropertyInfo item in propertyInfos)
{
if (tableHelper.Columns[j].ColumnName == item.Name)
{
if (tableHelper.Rows[i][j]!=DBNull.Value)
{
item.SetValue(type, tableHelper.Rows[i][j]);
}
else
{
item.SetValue(type, null);
}
}
}
}
rest.Add(type);
}
return rest;
}
8:什么Cookie?什么是Session? 两者有什么区别?
Cookie:网页浏览器用来保存用户信息的文件
Session:会话
Session是存储在服务器端的,Cookie是存储在客户端的
9:Cookie和Session的使用?
Cookie
Session["user"] = "majcms";
String username = Session["user"].ToString();
Session
//传值:
HttpCookie httpCookie = System.Web.HttpContext.Current.Response.Cookies.Add("USERINFO_USERNAME");
//得到cookie值
HttpCookie httpCookie = System.Web.HttpContext.Current.Request.Cookies.Get("USERINFO_USERNAME");
10:什么是路由?如何配置路由?
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 }
);
}
11:控制器的方法如何返回一个Json对象
①ActionResult-->JsonResult
②方法参数改为对象
③return View()-->return Json()
12:Ajax如何接受Json对象并做判断
success: function (data) {
if (data.Success) {
alert("注册成功");
window.location.href = "../Login/Login";
} else {
alert("注册失败");
window.location.href = "../Login/Register";
}
13:Ajax提交,Form表单需要注意什么问题
去除Form表单action属性提交
14:Sql语句参数化处理。
public bool UserInfoIsExists(string userName)
{
//准备sql
string sql = "select count(1) from UsetInfo where UserName=@UserName";
SqlParameter[] sqlParameter =
{
new SqlParameter() {
ParameterName="@UserName",
DbType=System.Data.DbType.String,
Value=userName
}
};
//执行sql
int num = (int)DBHelper.ExecuteScalar(sql, sqlParameter);
//处理返回结果
if (num != 1)
{
return true;
}
return false;
}