參數化sql命令--來自SqlHelper

一、數據庫操做函數web

using System.Data.SqlClient;

/// <summary>
    /// 鏈接數據庫
    /// </summary>
    /// <returns>返回SqlConnection對象</returns>
    public SqlConnection GetConnection()
    {
        //conn、ConnectionString在web.config
        //string myStr = ConfigurationManager.AppSettings["ConnectionString"].ToString();
        string myStr = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
        SqlConnection myConn = new SqlConnection(myStr);
        return myConn;
    }
/// <summary>
    ///執行一條不返回結果的SqlCommand,經過一個已經存在的數據庫鏈接 
    /// 使用參數數組提供參數
    /// </summary>
    /// <remarks>
    /// 使用示例:  
    ///  int result = ExecuteNonQuery(CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
    /// </remarks>
    /// <param name="conn">一個現有的數據庫鏈接</param>
    /// <param name="commandType">SqlCommand命令類型 (存儲過程, T-SQL語句, 等等。)</param>
    /// <param name="commandText">存儲過程的名字或者 T-SQL 語句</param>
    /// <param name="commandParameters">以數組形式提供SqlCommand命令中用到的參數列表</param>
    /// <returns>返回一個數值表示此SqlCommand命令執行後影響的行數</returns>
    public int ExecuteNonQuery(CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
    {
        SqlConnection myConn = GetConnection();
        SqlCommand cmd = new SqlCommand();
        PrepareCommand(cmd, myConn, null, cmdType, cmdText, commandParameters);
        int val = cmd.ExecuteNonQuery();
        cmd.Parameters.Clear();
        return val;
    }

/// <summary>
    /// 爲執行命令準備參數
    /// </summary>
    /// <param name="cmd">SqlCommand 命令</param>
    /// <param name="conn">已經存在的數據庫鏈接</param>
    /// <param name="trans">數據庫事物處理</param>
    /// <param name="cmdType">SqlCommand命令類型 (存儲過程, T-SQL語句, 等等。)</param>
    /// <param name="cmdText">Command text,T-SQL語句 例如 Select * from Products</param>
    /// <param name="cmdParms">返回帶參數的命令</param>
    private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdType, 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 = cmdType;
        if (cmdParms != null)
        {
            foreach (SqlParameter parm in cmdParms)
                cmd.Parameters.Add(parm);
        }
    }
二、傳入參數
using System.Data.SqlClient;

sql="insert into record(company,cas_no_all,image_all,ip_addr,date,remarks)
    values(@company,@cas_no_all,@image_all,@ip_addr,@date,@remarks)";
SqlParameter[] parameter = new SqlParameter[] { new SqlParameter("@company", SqlDbType.NVarChar), 
               new SqlParameter("@cas_no_all", SqlDbType.NVarChar), new SqlParameter("@image_all", SqlDbType.NText), 
               new SqlParameter("@ip_addr", SqlDbType.NVarChar), new SqlParameter("@date", SqlDbType.NVarChar), 
               new SqlParameter("@remarks", SqlDbType.NText) };

三、調用sql

ExecuteNonQuery(CommandType.Text, sql, parameter);

參考及SqlHelper延伸閱讀(包括MySql):
數據庫

http://baike.baidu.com/view/2765538.htm
參數化查詢好處:
一、防止sql注入式攻擊;
二、參數化查詢能夠查詢或寫入含有單引號的字符串;
...等等
延伸閱讀:
http://baike.baidu.com/view/3061939.htmc#

相關文章
相關標籤/搜索