11

好吧这是第一种方法

public static string srConnectionString = "server=localhost;database=myDB; "+
" uid=sa;pwd=myPW;";

这是第二种方法

public static string srConnectionString = "server=localhost;database=myDB; "+
" integrated security=SSPI;persist security info=False; Trusted_Connection=Yes;";

这两个连接字符串之间是否有任何性能差异或任何其他差异?

在这里我的 sql 连接类有什么建议吗?

using System;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data;

public static class DbConnection

{
    public static string srConnectionString = "server=localhost; database=myDB; uid=sa; pwd=myPW;";

    public static DataSet db_Select_Query(string strQuery)
    {
        DataSet dSet = new DataSet();
        if (strQuery.Length < 5)
            return dSet;
        try
        {
            using (SqlConnection connection = new SqlConnection(srConnectionString))
            {
                connection.Open();
                using (SqlDataAdapter DA = new SqlDataAdapter(strQuery, connection))
                {
                    DA.Fill(dSet);
                }
            }
            return dSet;
        }
        catch
        {
            using (SqlConnection connection = new SqlConnection(srConnectionString))
            {
                if (srConnectionString.IndexOf("select Id from tblAspErrors") != -1)
                {
                    connection.Open();
                    strQuery = strQuery.Replace("'", "''");

                    using (SqlCommand command = new SqlCommand("insert into tblSqlErrors values ('" + strQuery + "')", connection))
                    {
                        command.ExecuteNonQuery();
                    }
                }
            }
            return dSet;
        }
    }

    public static void db_Update_Delete_Query(string strQuery)
    {
        if (strQuery.Length < 5)
            return;
        try
        {
            using (SqlConnection connection = new SqlConnection(srConnectionString))
            {
                connection.Open();
                using (SqlCommand command = new SqlCommand(strQuery, connection))
                {
                    command.ExecuteNonQuery();
                }
            }
        }
        catch
        {
            strQuery = strQuery.Replace("'", "''");
            using (SqlConnection connection = new SqlConnection(srConnectionString))
            {
                connection.Open();
                using (SqlCommand command = new SqlCommand("insert into tblSqlErrors values ('" + strQuery + "')", connection))
                {
                    command.ExecuteNonQuery();
                }
            }
    }
}

}

4

1 回答 1

5

性能差异微不足道,可以忽略不计。身份验证检查是

  1. ...仅在登录时执行。不会为每个查询重新检查连接。然而,使用连接池,连接会被多次验证和重置,很可能是几乎每个查询
  2. ...与针对 Active Directory 检查域的每个文件访问和其他活动相同

仅供参考,这两个设置是相同的(使用一个或另一个):

集成安全=SSPI
Trusted_Connection=Yes

于 2012-12-19T19:55:54.340 回答