欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

數(shù)據(jù)庫(kù)SqlParameter 的插入操作,防止sql注入的實(shí)現(xiàn)代碼

 更新時(shí)間:2013年04月22日 15:11:29   作者:  
今天學(xué)習(xí)了一下SqlParameter的用法,原來(lái)這么寫是為了防止sql注入,破壞數(shù)據(jù)庫(kù)的。并自己動(dòng)手連接了數(shù)據(jù)庫(kù)。

例子:  點(diǎn)擊Button1按鈕的時(shí)候就把數(shù)據(jù)插入數(shù)據(jù)庫(kù)中。

復(fù)制代碼 代碼如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

namespace ParaMeter
{
    public partial class Test : System.Web.UI.Page
    {
        private string connectionStr;  //鏈接數(shù)據(jù)庫(kù)的字符串
        private SqlConnection conDB;   //數(shù)據(jù)庫(kù)的鏈接
        private SqlTransaction _trans; //事務(wù)對(duì)象

        protected void Page_Load(object sender, EventArgs e)
        {
            //connectionStr = ConfigurationSettings.AppSettings["constr"];
            connectionStr = "server=10.11.43.189\\SQL2008;database=OA_WEB_DB;uid=sa;pwd=123456";
            conDB = new SqlConnection(connectionStr);
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("INSERT INTO [OA_WEB_DB].[dbo].[OA_RT_FileType]([FileTypeName],[Deleted])");
            strSql.Append("VALUES(@fileName,@delete)");
            SqlParameter[] parameters = {
                                 new SqlParameter("@fileName", SqlDbType.NVarChar,100),
                                 new SqlParameter("@delete",SqlDbType.Bit),

                             };
            parameters[0].Value = "文件類型";
            parameters[1].Value = false;
          bool IsSucc =   ExecUpdateSql(strSql.ToString(), parameters);
          if (IsSucc)
          {
             Label1.Text =  "插入成功";
          }
          else
          {
              Label1.Text = "插入失敗";
          }

        }
        /// 執(zhí)行一條更新語(yǔ)句
        /// </summary>
        /// <param name="SQLString">需要執(zhí)行的SQL語(yǔ)句。</param>
        /// <param name="cmdParms">執(zhí)行參數(shù)數(shù)組</param>
        /// <returns>成功返回True,失敗返回False。</returns>
        private bool ExecUpdateSql(string SQLString, params SqlParameter[] cmdParms)
        {
            using (SqlCommand cmd = new SqlCommand())
            {
                try
                {
                    PrepareCommand(cmd, conDB, _trans, SQLString, cmdParms);
                    int iret = cmd.ExecuteNonQuery();
                    return true;
                }
                catch (System.Data.SqlClient.SqlException e)
                {
                    return false;
                }
            }
        }
        private void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, string cmdText, SqlParameter[] cmdParms)
        {
            if (conn.State != ConnectionState.Open)
                conn.Open();
            cmd.Connection = conn;
            cmd.CommandText = cmdText;
            if (trans != null)
                cmd.Transaction = trans;
            cmd.CommandType = CommandType.Text;//cmdType;
            if (cmdParms != null)
            {
                foreach (SqlParameter parameter in cmdParms)
                {
                    if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) &&
                        (parameter.Value == null))
                    {
                        parameter.Value = DBNull.Value;
                    }
                    cmd.Parameters.Add(parameter);
                }
            }
        }

    }
}

相關(guān)文章

  • 使用正則Regex來(lái)移除網(wǎng)頁(yè)的EnableViewState實(shí)現(xiàn)思路及代碼

    使用正則Regex來(lái)移除網(wǎng)頁(yè)的EnableViewState實(shí)現(xiàn)思路及代碼

    創(chuàng)建好網(wǎng)頁(yè)時(shí),什么都沒(méi)有寫,但運(yùn)行時(shí)會(huì)發(fā)現(xiàn)源程序(View Source),下面一段,此刻,也許你會(huì)想起,在網(wǎng)頁(yè)有一個(gè)屬性EnableViewState,在某些時(shí)候我們并不需要它,接下來(lái)將介紹如何移除它,感興趣的朋友可以了解下啊
    2013-01-01
  • WPF使用StackPanel棧面板布局

    WPF使用StackPanel棧面板布局

    這篇文章介紹了WPF使用StackPanel棧面板布局的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-02-02
  • 利用.net控件實(shí)現(xiàn)下拉導(dǎo)航菜單制作的具體方法

    利用.net控件實(shí)現(xiàn)下拉導(dǎo)航菜單制作的具體方法

    這篇文章介紹了利用.net控件實(shí)現(xiàn)下拉導(dǎo)航菜單制作的具體方法,有需要的朋友可以參考一下,希望對(duì)你有所幫助
    2013-07-07
  • .Net Core內(nèi)存回收模式及性能測(cè)試對(duì)比分析

    .Net Core內(nèi)存回收模式及性能測(cè)試對(duì)比分析

    下面小編就為大家分享一篇.Net Core內(nèi)存回收模式及性能測(cè)試對(duì)比分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2017-12-12
  • Asp.net靜態(tài)方法之Grid轉(zhuǎn)DataTable方法實(shí)現(xiàn)步驟

    Asp.net靜態(tài)方法之Grid轉(zhuǎn)DataTable方法實(shí)現(xiàn)步驟

    GridView綁定DataTable后,如何獲取GridView綁定后顯示的值,在項(xiàng)目需求的背景下寫了一個(gè)靜態(tài)方法,經(jīng)過(guò)在項(xiàng)目中的使用,bug的修復(fù),較為穩(wěn)定
    2013-04-04
  • C#默認(rèn)以管理員身份運(yùn)行程序?qū)崿F(xiàn)代碼

    C#默認(rèn)以管理員身份運(yùn)行程序?qū)崿F(xiàn)代碼

    權(quán)限不夠,導(dǎo)致無(wú)法修改系統(tǒng)時(shí)間,于是我以管理員身份運(yùn)行了一次,結(jié)果測(cè)試成功,下面為大家介紹下C#如何默認(rèn)以管理員身份運(yùn)行程序
    2014-03-03
  • .net core中編輯json配置文件的方法

    .net core中編輯json配置文件的方法

    今天給大家分享.net core中編輯json配置文件的方法,在項(xiàng)目設(shè)計(jì)中,需要在運(yùn)行過(guò)程中,將遠(yuǎn)程服務(wù)端發(fā)送過(guò)來(lái)的配置信息回寫到配置文件中,具體內(nèi)容詳情跟隨小編一起看看吧
    2021-06-06
  • 偽靜態(tài)web.config配置步驟

    偽靜態(tài)web.config配置步驟

    web.config是asp.net一個(gè)重要的配置文件,本文將介紹如何利用web.config配置偽靜態(tài),步驟很詳細(xì),需要了解的朋友可以參考下
    2012-12-12
  • asp.net core配合vue實(shí)現(xiàn)后端驗(yàn)證碼邏輯

    asp.net core配合vue實(shí)現(xiàn)后端驗(yàn)證碼邏輯

    網(wǎng)上的前端驗(yàn)證碼邏輯總感覺(jué)不安全,驗(yàn)證碼建議還是使用后端配合驗(yàn)證。本文主要介紹了asp.net core配合vue實(shí)現(xiàn)后端驗(yàn)證碼邏輯,感興趣的可以了解一下
    2021-06-06
  • ASP.NET中Request.Form中文亂碼的解決方法

    ASP.NET中Request.Form中文亂碼的解決方法

    一直以為,只有 Request.QueryString 接收url查詢字符串含中文時(shí)可能會(huì)亂碼(做好編解碼工作,很容易可以避免),碰到的時(shí)候,才知道 Request.Form 接收表單的值中含中文也會(huì)亂碼,以前真是很傻很天真很無(wú)知。
    2011-03-03

最新評(píng)論