这个 C# 4.0 MSSQL 2008 R2 数据库连接类可以改进吗 - 专家问题
这是我的通用数据库连接类。我正在使用此类通过网站执行查询。您对此有何建议以提高性能?谢谢。
MSSQL 2008 R2 SP1 - Microsoft Visual Studio 2010 SP1、C# 4.0 - ASP.net 4.0
类
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Web;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data;
using System.IO;
/// <summary>
/// Summary description for DbConnection
/// </summary>
public class DbConnection
{
public static string srConnectionString = "server=localhost;database=myDB;uid=sa;pwd=MYPW;";
public DbConnection()
{
}
public static DataSet db_Select_Query(string strQuery)
{
DataSet dSet = new DataSet();
try
{
using (SqlConnection connection = new SqlConnection(srConnectionString))
{
connection.Open();
SqlDataAdapter DA = new SqlDataAdapter(strQuery, connection);
DA.Fill(dSet);
}
return dSet;
}
catch (Exception)
{
using (SqlConnection connection = new SqlConnection(srConnectionString))
{
if (srConnectionString.IndexOf("select Id from tblAspErrors") != -1)
{
connection.Open();
strQuery = strQuery.Replace("'", "''");
SqlCommand command = new SqlCommand("insert into tblSqlErrors values ('" + strQuery + "')", connection);
command.ExecuteNonQuery();
}
}
return dSet;
}
}
public static void db_Update_Delete_Query(string strQuery)
{
try
{
using (SqlConnection connection = new SqlConnection(srConnectionString))
{
connection.Open();
SqlCommand command = new SqlCommand(strQuery, connection);
command.ExecuteNonQuery();
}
}
catch (Exception)
{
strQuery = strQuery.Replace("'", "''");
using (SqlConnection connection = new SqlConnection(srConnectionString))
{
connection.Open();
SqlCommand command = new SqlCommand("insert into tblSqlErrors values ('" + strQuery + "')", connection);
command.ExecuteNonQuery();
}
}
}
}
This is my general database connection class. I am using this class to execute my queries through website. What would your suggestions about this to improve performance. Thank you.
MSSQL 2008 R2 SP1 - Microsoft Visual Studio 2010 SP1 , C# 4.0 - ASP.net 4.0
Class
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Web;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data;
using System.IO;
/// <summary>
/// Summary description for DbConnection
/// </summary>
public class DbConnection
{
public static string srConnectionString = "server=localhost;database=myDB;uid=sa;pwd=MYPW;";
public DbConnection()
{
}
public static DataSet db_Select_Query(string strQuery)
{
DataSet dSet = new DataSet();
try
{
using (SqlConnection connection = new SqlConnection(srConnectionString))
{
connection.Open();
SqlDataAdapter DA = new SqlDataAdapter(strQuery, connection);
DA.Fill(dSet);
}
return dSet;
}
catch (Exception)
{
using (SqlConnection connection = new SqlConnection(srConnectionString))
{
if (srConnectionString.IndexOf("select Id from tblAspErrors") != -1)
{
connection.Open();
strQuery = strQuery.Replace("'", "''");
SqlCommand command = new SqlCommand("insert into tblSqlErrors values ('" + strQuery + "')", connection);
command.ExecuteNonQuery();
}
}
return dSet;
}
}
public static void db_Update_Delete_Query(string strQuery)
{
try
{
using (SqlConnection connection = new SqlConnection(srConnectionString))
{
connection.Open();
SqlCommand command = new SqlCommand(strQuery, connection);
command.ExecuteNonQuery();
}
}
catch (Exception)
{
strQuery = strQuery.Replace("'", "''");
using (SqlConnection connection = new SqlConnection(srConnectionString))
{
connection.Open();
SqlCommand command = new SqlCommand("insert into tblSqlErrors values ('" + strQuery + "')", connection);
command.ExecuteNonQuery();
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
1.) 如何确保传入的 strQuery 不会受到 sql 注入?
2.) 使用日志框架,如 nlog 或 log4net。这将允许您轻松地通过使用配置文件来指定存储错误日志的位置(文件、电子邮件、数据库)。
您的日志记录将如下所示:
3.) 使用 SecureString
4.) 如果数据库关闭,您将如何将错误写入数据库?它会生成一个未捕获的异常...
1.) How are you making sure the strQuery passed in isn't subjected to sql injection?
2.) Use a logging framework like nlog or log4net. This will allow you to easily dictate where to store the error logs (file, email, db) just by using a config file.
your logging would be something like this instead:
3.) Use SecureString
4.) How are you going to write the error to the DB if the DB is down? It'll generate an uncaught exception...