• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
定不负相思懿
博客园    首页    新随笔    联系   管理    订阅  订阅
参数化查询(简单举例)

  这几天在查一些有关SQL语句防注入的资料,敲敲改改总算弄好了,不多说,贴代码

string str = @"server=LAPTOP-CM9CUARS;Integrated Security=SSPI;database=Space;";
            using (SqlConnection Conn = new SqlConnection(str))
            {
                Conn.Open(); //打开数据库 
                try
                {
                    using (SqlCommand Cmd = Conn.CreateCommand())
                    {
                        Cmd.CommandText = "select * from tabUsers where ID=@ID and hspwd=@hspwd";
                        Cmd.Parameters.Add(new SqlParameter("@ID", ID));
                        Cmd.Parameters.Add(new SqlParameter("@hspwd", hspwd1));
                        int count = Convert.ToInt32(Cmd.ExecuteScalar());

                        if (count > 0)
                        { 
                            Session["ID1"] = ID;
                            string sql = "select*from tabUsers where id='" + ID + "'";
                            string name = Class.Search(sql);
                            Session["name"] = name;
                            Response.Write("<script>alert('登录成功!');location='Space.aspx'</script>");
                        }
                        else
                            Response.Write("<script>alert('登录失败,请正确填写账号、密码!')</script>");
                    }
                }

posted on 2016-12-04 18:30  定不负相思懿  阅读(607)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3