工具类如何写一个增删改的方法?
public bool EquSql(string sql, SqlParameter[] parameters)
{
bool result = false;
SqlConnection connection = null;
try
{
connection = new SqlConnection(conser);
connection.Open();
SqlCommand sqlCommand = new SqlCommand(sql, connection);
sqlCommand.Parameters.AddRange(parameters);
int sum = sqlCommand.ExecuteNonQuery();
result = sum > 0;
return result;
}
catch (Exception)
{
return result;
}
finally
{
connection.Close();
}
}
2:如何写向表里面插入一条记录的sql?
insert into 表名(列名)values(‘添加的数据’)
3:怎么查询一张表的记录?
select * from 表名
4:Ajax的提交格式?
$.ajax({
type: "POST",
url: url,
data: data,
success: success,
dataType: dataType
});
5:工具类如何写一个查询的方法?
public int SabEquSql(string sql, SqlParameter[] parameters)
{
SqlConnection connection = null;
try
{
connection = new SqlConnection(conser);
connection.Open();
SqlCommand sqlCommand = new SqlCommand(sql, connection);
for (int i = 0; i < parameters.Length; i++)
{
sqlCommand.Parameters.Add(parameters[i]);
}
SqlDataReader result = sqlCommand.ExecuteReader();
if (result.Read())
{
return 1;
}
return -1;
}
catch (Exception)
{
return -1;
}
finally
{
connection.Close();
}
}
6:如何引入第三方类库?
(1)打开模板创建一个新项目,项目的后缀名为Romte
(2)下载文件压缩包解压把项目放在项目里,安装里面的包,然后在配置信息
(3)最后通过业务层调用(new)第三方类去调用发送短信的方法
7:WebConfig如何配置变量的值? 后台如何读取?
把放在Web.config里ConfigurationManager.AppSettings[“appId”]进行读取
8:第三方的项目Remote
创建项目Remote用来调用第三方提供的类
执行“引用”右键点击“管理NuGit程序包”,随后在“浏览”中添加第三方的引用类库
最后封装相对应的属性
9:WebConfig如何配置变量的值? 后台如何读取?
把放在Web.config里ConfigurationManager.AppSettings[“appId”]进行读取
10:工具类如何写一个数据集的方法?
public static DataSet Query(string sql, SqlParameter[] parameters)
{
SqlConnection connection = null;
DataSet dataSet = new DataSet();
try
{
connection = new SqlConnection(connStr);
connection.Open();
SqlCommand sqlCommand = new SqlCommand(sql, connection);
if (parameters!=null)
{
sqlCommand.Parameters.AddRange(parameters);
}
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
sqlDataAdapter.Fill(dataSet);
return dataSet;
catch (Exception ex)
{
return dataSet;
}
finally
{
connection.Close();
}
}
11:数据集如何转换成List [知识点:反射]
public class DataTableHelper
{
public static List<T> DataTableList<T>(DataTable dt)
{
List<T> result = 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 info in propertyInfos)
{
if (dt.Columns[j].ColumnName==info.Name)
{
info.SetValue(type, dt.Rows[i][j]);
}
else
{
info.SetValue(type,null);
}
}
}
result.Add(type);
}
return result;
}
}
12:什么Cookie?什么是Session? 两者有什么区别?
cookie 是一种发送到客户浏览器的文本串句柄,并保存在客户机硬盘上,可以用来在某个WEB站点会话间持久的保持数据。
session 指的就是访问者从到达某个特定主页到离开为止的那段时间。
cookie和session都是用来跟踪浏览器用户身份的会话方式。
cookie 和session的区别是:cookie数据保存在客户端,session数据保存在服务器端
13:Cookie和Session的使用?
cookie
HttpCookie cookie =new HttpCookie("tempMsg");
cookie.Values["Email"] = "soaeon@163.com";
cookie.Values["key"] = "soaeon" ;
cookie.Domain = "soaeon.com"; //设置当前cookie所属于的域
cookie.Expires = (DateTime)expiresTime //设置cookie的过期时间(持久cookie)
System.Web.HttpContext.Current.Response.Cookies.Set(cookie);
//取值:
HttpCookie cookie = System.Web.HttpContext.Current.Request.Cookies["tempMsg"];
string email = cookie.Values["Email"];
string key = cookie.Values["key"];
session
Session["name"] = "soaeon@163.com";
if (Session["name"] != null)
{
string str = Session["name"].ToString();
}
14:什么是路由?如何配置路由?
路由就是一个路径的解析,根据客户端提交的路径,将请求解析到相应的控制器上;
从 URL 找到处理这个 URL 的类和函数。
配置路由时
项目下的Contorls文件夹下的ValuesController会出现一些方法,但webapi请求语法并不是那么的好用,所以我们先修改路由规则,使其更符合我们平常使用MVC的设计习惯:
(1) 将routeTemplate: “api/{controller}/{id}”,修改为routeTemplate: “api/{controller}/{action}/{id}”,
(2) 之后你就发现我们也需要传方法名称才可以到指定的方法
15:控制器的方法如何返回一个Json对象
(1)首先将 ActionResult 改成 JsonResult
(2) return json(结果);
15:Ajax如何接受Json对象并做判断
$("#submit").on("click", function () {
var date = {};
date.UserName = $("#username").val();
date.UserPwd = $("#password").val();
date.UserMible = $("tel").val();
$.ajax({
url: "/Home/Action",
type: "post",
data:date,
success: function (sele) {
if (sele.Pandun) {
alert("注册成功");
window.location.href = "/Home/Login";
} else {
alert("注册失败");
}
}
})
})
16:Ajax提交,Form表单需要注意什么问题
要把from表单删除,原因是按钮提交的是错误信息
17:Sql语句参数化处理
$"select * form stunent where Id='{id}'"
sql语句:"select * form stunent where Id=@id"
SqlParamenter[] pars={
new SqlParamenter(){
DBtype=System.Data.DbType.String ,
ParametnerName=@id,
Value=对象.Id属性
}
}