MVC基础

本文涵盖了ASP.NET MVC的基础知识,包括工具类中增删改的方法实现,SQL语句的插入与查询,Ajax提交的格式,数据集转List<T>的反射应用,Cookie与Session的对比及使用,路由配置,控制器返回Json对象,以及Ajax与Form表单的交互注意事项,强调了Sql语句的参数化处理以提高安全性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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();
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值