惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

腾讯CDC
GbyAI
GbyAI
C
CERT Recently Published Vulnerability Notes
S
Security @ Cisco Blogs
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
The Hacker News
The Hacker News
D
Darknet – Hacking Tools, Hacker News & Cyber Security
P
Proofpoint News Feed
C
Cyber Attacks, Cyber Crime and Cyber Security
S
Securelist
Security Latest
Security Latest
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Simon Willison's Weblog
Simon Willison's Weblog
Latest news
Latest news
T
Tor Project blog
T
Threat Research - Cisco Blogs
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Spread Privacy
Spread Privacy
K
Kaspersky official blog
T
The Exploit Database - CXSecurity.com
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
V2EX - 技术
V2EX - 技术
L
Lohrmann on Cybersecurity
Google Online Security Blog
Google Online Security Blog
Cyberwarzone
Cyberwarzone
Help Net Security
Help Net Security
The Last Watchdog
The Last Watchdog
C
Cybersecurity and Infrastructure Security Agency CISA
Attack and Defense Labs
Attack and Defense Labs
大猫的无限游戏
大猫的无限游戏
Schneier on Security
Schneier on Security
H
Heimdal Security Blog
O
OpenAI News
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Visual Studio Blog
AI
AI
H
Hacker News: Front Page
博客园_首页
博客园 - 【当耐特】
L
LINUX DO - 最新话题
MyScale Blog
MyScale Blog
量子位
Vercel News
Vercel News
C
Cisco Blogs
L
LINUX DO - 热门话题
Y
Y Combinator Blog
T
Threatpost
爱范儿
爱范儿
P
Privacy & Cybersecurity Law Blog

博客园 - 站在天空下的猪

SqlBulkCopy批量转移数据备注 根据文本框联动下拉框(jquery 插件) - 站在天空下的猪 - 博客园 关于用FOMR提交编码的问题 jquery 图片预览插件 - 站在天空下的猪 - 博客园 自己写的一个jquery模板引擎(json比较好用) Document 对象的常用方法 今天发现梅花雨日历控件蛮好用的哟,腾讯都在用 身份证对应县及县的行政区划代码 【SQLSERVER】存储过程基础 终于解决了一个AjaxPro无刷新的问题了,可以引用在短信方面滴asp.net 2.0 昨天很失败 一个简单的FileUpload处理逻辑 C# 判断是否为数字 DataBinder.Eval总结 asp.net 2.0中实现防盗链 如何取得IP/用户名等信息 “ConnectionString 属性尚未初始化”的另类解决办法 asp.net常用函数表 C#中计算两个时间的差
如何把HTML语句直接保存到数据库
站在天空下的猪 · 2007-05-16 · via 博客园 - 站在天空下的猪

默认情况下,出于安全的考虑,C#.net页面是不允许直接上传HTML语法的。但是有一种方法可以安全的实现数据上传。下面用一个实例说明如何实现:
 
 using System.Data.SqlClient;
 
     static string connectionString = ConfigurationManager.AppSettings["ConnectionString"];
     
 public bool InsertNews(string title, string content, string type, string created)
     {
      //在下面的sql语句中,使用了变量@title和@content
         string sql = "Insert Into News (title,content,type,created,createdate) Values(@title,@content," + type + ",'" + created + "','" + DateTime.Now.ToString() + "')";
         SqlConnection conn = new SqlConnection(connectionString);
         SqlCommand cmd = new SqlCommand(sql, conn);
         //注意下面四句
         cmd.Parameters.Add(new SqlParameter("@title", SqlDbType.VarChar, 100)); //定义变量@title
         cmd.Parameters["@title"].Value = title;     //变量@title赋值
         cmd.Parameters.Add(new SqlParameter("@content", SqlDbType.NText)); //定义变量@content
         cmd.Parameters["@content"].Value = content;    //变量@content赋值
         
         conn.Open();
         int flag = cmd.ExecuteNonQuery();
         conn.Close();
         if (flag != -1)
              return true;
         else
              return false;
     }
     
     C#.net的页面默认是不允许HTML语法直接传送到后台的,所以即使如上面的写法,仍然无法实现数据上传。用TextBox也不行。
     需要在aspx页面<%@page %>中增加参数定义:ValidateRequest="false"。写法如下:
     <%@ Page ValidateRequest="false" Language="C#" AutoEventWireup="true" CodeFile="NewsContent.aspx.cs" Inherits="Manage_NewsContent" %>
     
     这种写法把前台传过来的数据完全当作数据写进sql语句,所以也不用担心sql注入入侵。即使是单引号、双引号也没有问题,完全不会影响sql语句的执行。
     个人觉得,是一中值得推广的好方法。

posted on 2007-05-16 07:35  站在天空下的猪  阅读(1967)  评论()    收藏  举报