如connection="......"
等,使用很频繁的变量,不知道大家是怎么用比较安全简易的办法做的?

解决方案 »

  1.   

    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Collections;
    namespace SqlDAL
    {
    /// <summary>
    ///数据操作工具类
    /// </summary>
    public sealed class SQLHelper
    {
    //数据库连接字符串
    public static readonly string CONN_STRING =System.Configuration.ConfigurationSettings.AppSettings["data"];
    /// <summary>
    /// Execute a SqlCommand (that returns no resultset) against the database specified in the connection string 
    /// using the provided parameters.
    /// </summary>
    /// <res>
    /// e.g.:  
    ///  int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
    /// </res>
    /// <param name="connectionString">a valid connection string for a SqlConnection</param>
    /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
    /// <param name="commandText">the stored procedure name or T-SQL command</param>
    /// <param name="commandParameters">an array of SqlParamters used to execute the command</param>
    /// <returns>an int representing the number of rows affected by the command</returns>
    public static int ExecuteNonQuery(string connString, CommandType cmdType, string cmdText, params SqlParameter[] cmdParms) 
    { SqlCommand cmd = new SqlCommand(); using (SqlConnection conn = new SqlConnection(connString)) 
    {
    PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
    int val = cmd.ExecuteNonQuery();
    cmd.Parameters.Clear();
    return val;
    }
    } /// <summary>
    /// Execute a SqlCommand (that returns no resultset) using an existing SQL Transaction 
    /// using the provided parameters.
    /// </summary>
    /// <res>
    /// e.g.:  
    ///  int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
    /// </res>
    /// <param name="trans">an existing sql transaction</param>
    /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
    /// <param name="commandText">the stored procedure name or T-SQL command</param>
    /// <param name="commandParameters">an array of SqlParamters used to execute the command</param>
    /// <returns>an int representing the number of rows affected by the command</returns>
    public static int ExecuteNonQuery(SqlTransaction trans, CommandType cmdType, string cmdText, params SqlParameter[] cmdParms) 
    {
    SqlCommand cmd = new SqlCommand();
    PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, cmdParms);
    int val = cmd.ExecuteNonQuery();
    cmd.Parameters.Clear();
    return val;
    }
    /// <summary>
    /// Execute a SqlCommand that returns a resultset against the database specified in the connection string 
    /// using the provided parameters.
    /// </summary>
    /// <res>
    /// e.g.:  
    ///  SqlDataReader r = ExecuteReader(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
    /// </res>
    /// <param name="connectionString">a valid connection string for a SqlConnection</param>
    /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
    /// <param name="commandText">the stored procedure name or T-SQL command</param>
    /// <param name="commandParameters">an array of SqlParamters used to execute the command</param>
    /// <returns>A SqlDataReader containing the results</returns>
    public static SqlDataReader ExecuteReader(string connString, CommandType cmdType, string cmdText, params SqlParameter[] cmdParms) 
    {
    SqlCommand cmd = new SqlCommand();
    SqlConnection conn = new SqlConnection(connString);
    try 
    {
    PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
    SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
    cmd.Parameters.Clear();
    return rdr;
    }
    catch 
    {
    conn.Close();
    throw;
    }
    }
    /// <summary>
    /// Prepare a command for execution
    /// </summary>
    /// <param name="cmd">SqlCommand object</param>
    /// <param name="conn">SqlConnection object</param>
    /// <param name="trans">SqlTransaction object</param>
    /// <param name="cmdType">Cmd type e.g. stored procedure or text</param>
    /// <param name="cmdText">Command text, e.g. Select * from Products</param>
    /// <param name="cmdParms">SqlParameters to use in the command</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);
    }
    }
    }
    }
      

  2.   

    //加上  using System.Text;
    StringBuilder connectionString = new StringBuilder(@"Provider=Microsoft.Jet.OLEDB.4.0;");
    connectionString.Append(@"Data Source=c:\joseph\joseph.mdb;");
    connectionString.Append("Persist Security Info = False");