今天马哥找来何键桥师兄给分析如何防SQL注入,师兄不耐其烦的给讲了存储过程的简单运用。
就拿一个简单插入语句做例子,把新闻标题和内容插入数据库News表
先在数据库里写一个存储过程

create proc AddNews
(
  
@title varchar(50),
  
@content text
)
as
insert into News values(@title,@content)

然后在后台页面里写这样的代码:
 protected void Button1_Click(object sender, EventArgs e)
    
{
        
string strConn = ConfigurationManager.ConnectionStrings["seaConnectionString"].ConnectionString;
        SqlConnection con 
= new SqlConnection(strConn);
        
//string sql = "insert into news (NewsTitle,NewsContent) values ('" + this.TextBox1.Text + "','" + this.TextBox2.Text + "')"
        SqlParameter[] paras = new SqlParameter[2];
        paras[
0= new SqlParameter("@title", SqlDbType.VarChar, 50);
        paras[
0].Value = this.TextBox1.Text;
        paras[
1= new SqlParameter("@content",SqlDbType.Text);
        paras[
1].Value = TextBox2.Text;
        SqlCommand cmd 
= new SqlCommand(AddNews, con);
        cmd.CommandType 
= CommandType.StoredProcedure;
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
        Response.Write(
"OK!");

    }

这样就实现了注释的语句所能实现的功能。
一个用存储过程做的数据库操作完成了。
posted on 2007-09-27 01:21  超少  阅读(204)  评论(1编辑  收藏  举报