1:工具类如何写一个增删改的方法?
public static int ExecuteNonQuery(string sql)
{
//创建数据库连接
SqlConnection conn = new SqlConnection(strConn);
try
{
//打开数据库
conn.Open();
//创建Command命令
SqlCommand comm = new SqlCommand(sql, conn);
//返回结果
return comm.ExecuteNonQuery();
}
catch (Exception)
{
throw;
}
finally
{
//关闭数据库连接
conn.Close();
}
}
2:如何写向表里面插入一条记录的sql?
INSERT INTO 表明 values(值1,值2)
3:怎么查询一张表的记录?
public static SqlDataReader ExecuteReader(string sql)
{
//创建数据库连接
SqlConnection conn = new SqlConnection(strConn);
try
{
//打开数据库
conn.Open();
//创建Command命令
SqlCommand comm = new SqlCommand(sql, conn);
//返回结果
return comm.ExecuteReader(CommandBehavior.CloseConnection);
}
catch (Exception)
{
throw;
}
}
4:Ajax的提交格式?
$.ajax({
url:"../regionservlet",
data:{"paid":-1},
dataType:"json",
type:"post",
success:function(){
5:工具类如何写一个查询的方法?
public static object ExecuteScalar(string sql)
{
//创建数据库连接
SqlConnection conn = new SqlConnection(strConn);
try
{
//打开数据库
conn.Open();
//创建Command命令
SqlCommand comm = new SqlCommand(sql, conn);
//返回结果
return comm.ExecuteScalar();
}
catch (Exception)
{
throw;
}
finally
{
//关闭数据库连接
conn.Close();
}
6:如何引入第三方类库?
引用右键添加引用 在右下脚有个浏览 点击找到第三方类库
7:WebConfig如何配置变量的值? 后台如何读取?
在appSettings里面添加 存储<add key="k" value="v" /> 读取ConfigurationManager.AppSettings["k"]
8:工具类如何写一个数据集的方法?
public static DataView BindingView(string sql)
{
DataSet ds = new DataSet();
//准备SQL语句
SqlDataAdapter sda = new SqlDataAdapter(sql, DBHelper.Connection);
//填充数据
sda.Fill(ds);
//创建DataView
DataView dv = new DataView(ds.Tables[0]);
//返回数据
return dv;
9:数据集如何转换成List<T> [知识点:反射]
public IList <T> GetList <T>(DataTable table)
{
IList <T> list = new List <T>();
T t = default(T);
PropertyInfo[] propertypes = null;
string tempName = string.Empty;
foreach (DataRow row in table.Rows)
{
t = Activator.CreateInstance <T>();
propertypes = t.GetType().GetProperties();
foreach (PropertyInfo pro in propertypes)
{
tempName = pro.Name;
if (table.Columns.Contains(tempName))
{
object value = row[tempName];
pro.SetValue(t, value, null);
}
}
list.Add(t);
}
return list;
}
10:什么Cookie?什么是Session? 两者有什么区别?
Cookie存储在客户端的 Session存储在每次绘画种关闭就没有了
11:Cookie和Session的使用?
//使用cookie
HttpCookie cookie = new HttpCookie("KEY");
cookie["KEY"] = "值";
System.Web.HttpContext.Current.Response.Cookies.Add(cookie);
//使用Session
HttpContext.Session["KEY"] = VALUE
12:什么是路由?如何配置路由?
路由在App_Start文件夹 RouteConfig类, 在defaults: new { controller = "控制器", action = "方法", id = UrlParameter.Optional } 设置网页起始位置
13:控制器的方法如何返回一个Json对象
public JsonResult 方法名()
{
return Json();
}
14:Ajax如何接受Json对象并做判断
$.ajax({
url: '路径',
type: 'Post',
data: Json对象,
success: function (data) {
if (data.Success) {
alert("成功")
} else {
alert("失败")
}
}
})
15:Ajax提交,Form表单需要注意什么问题
删除Form表单
16:Sql语句参数化处理。
//第一步:声明数据库连接对象:
Sqlconnection connection=new Sqlconnection(ConnectionString);
//第二步:声明数据库操作对象:
两种途径:
//直接以字符串拼接的方式形成sql语句,比如:
sqlstr="insert into usertab(uid,pwd) values('"+uidtxt+"','"+pwdtxt+"')";
SqlCommand command = new SqlCommand(sqlstr, connection);
//以参数占位的先行成形式语句,然后对参数实行绑定,比如:
sqlstr="insert into usertab(uid,pwd) values(@uidtxt,@pwdtxt)";
SqlCommand command = new SqlCommand(sqlstr, connection);
command.Parameters.Add("@uidtxt", SqlDbType.Text);
command.Parameters["@uidtxt"].Value =uidtxt;
command.Parameters.Add("@pwdtxt", SqlDbType.Text);
command.Parameters["@pwdtxt"].Value =uidtxt;
//第三步:执行数据库操作:
command.ExecuteNonQuery();
connection.close();