Tag: sqlconnection

哪种模式更适合SqlConnection对象?

哪种模式更适合SqlConnection对象? 哪个性能更好? 你提供任何其他模式吗? class DataAccess1 : IDisposable { private SqlConnection connection; public DataAccess1(string connectionString) { connection = new SqlConnection(connectionString); } public void Execute(string query) { using (SqlCommand command = connection.CreateCommand()) { command.CommandText = query; command.CommandType = CommandType.Text; // … command.Connection.Open(); command.ExecuteNonQuery(); command.Connection.Close(); } } public void Dispose() { connection.Dispose(); } } VS class DataAccess2 : […]

在c#中创建一个IDisposable类,在完成时清理SqlConnection

在回答上一个问题时,有人建议: 让SqlConnection成为你的类的成员变量,但是当类处理时,使类IDisposable并处理SqlConnection 我已经把这个建议的实现放在一起(下面),但是想检查一下这个实现是否正确(显然它除了打开连接之外当前没有做任何事情但是想法是那里会有使用连接的方法哪个能够依赖现有的和开放的。 public class DatabaseRecord : IDisposable { protected SqlConnection connection; public DatabaseRecord() { connection = new SqlConnection(“ConnectionString”); connection.Open(); } // IDisposable implementation private bool disposed; public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } private void Dispose(bool disposing) { if (!this.disposed) { if (disposing) { connection.Dispose(); } disposed = true; } } // Destructor ~DatabaseRecord() […]

c#关闭sqlconnection和sqldatareader与否?

我有这段代码: SqlConnection conn; string strconString = System.Configuration.ConfigurationManager.ConnectionStrings[“SQLCONN”].ToString(); conn = new SqlConnection(strconString); string cmdstr = “select status from racpw where vtgid = ” + vtgid; SqlCommand cmdselect = new SqlCommand(cmdstr, conn); conn.Open(); SqlDataReader dtr = cmdselect.ExecuteReader(); if (dtr.Read()) { return; } else { … } dtr.Close(); conn.Close(); 现在我的问题是。 如果返回,我的连接和dtr是自动关闭还是我应该使用bool变量并在我的连接关闭后执行返回?

为什么不处理/关闭SqlConnection?

鉴于方法: internal static DataSet SelectDataSet(String commandText, DataBaseEnum dataBase) { var dataset = new DataSet(); SqlConnection sqlc = dataBase == DataBaseEnum.ZipCodeDb ? new SqlConnection(ConfigurationManager.AppSettings[“ZipcodeDB”]) : new SqlConnection(ConfigurationManager.AppSettings[“WeatherDB”]); SqlCommand sqlcmd = sqlc.CreateCommand(); sqlcmd.CommandText = commandText; var adapter = new SqlDataAdapter(sqlcmd.CommandText, sqlc); adapter.Fill(dataset); return dataset; } 为什么sqlc(SqlConnection)在调用方法超出范围后没有处理/关闭,或者sqlc没有更多的引用? 编辑1:即使将其包装在使用中,我仍然可以看到连接使用(我已关闭连接池): SELECT DB_NAME(dbid) as ‘Database Name’, COUNT(dbid) as ‘Total Connections’ FROM […]

.Net SqlConnection,服务器身份validation和证书固定

使用s SqlConnection时如何固定证书? 从SqlConnection连接字符串参数关键字和值 ,我知道我可以将Encrypted设置为true以强制(鼓励?)使用SSL / TLS。 但是,为了固定证书,我认为我们需要使用ServicePointManager ServerCertificateValidationCallback (下面的示例代码由ArneVajhøj提供,用于HTTP / HTTPS)。 我不清楚如何将PinCertificate (从ServicePointManager )连接到SqlConnection 。 更新:在microsoft.public.dotnet.languages.csharp上与ArneVajhøj交谈,似乎无法对连接进行所需的控制。 Vajhøj提供了一个指向加密SQL Server连接的链接。 public static void Main(string[] args) { ServicePointManager.ServerCertificateValidationCallback = PinCertificate; WebRequest wr = WebRequest.Create(“https://www.google.com/”); wr.GetResponse(); } public static bool PinCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { byte[] chash = certificate.GetCertHash(); StringBuilder sb = new StringBuilder(chash.Length * […]

您应该重用SqlConnection,SqlDataAdapter和SqlCommand对象吗?

我正在使用以类似于以下代码的布局编写的DAL对象。 我简化了很多代码只是为了显示设置。 public class UserDatabase : IDisposable { private SqlDataAdapter UserDbAdapter; private SqlCommand UserSelectCommand; private SqlCommand UserInsertCommand; private SqlCommand UserUpdateCommand; private SqlCommand UserDeleteCommand; private System.Data.SqlClient.SqlConnection SQLConnection; public UserDatabase() { this.SQLConnection = new System.Data.SqlClient.SqlConnection(ConnectionString); this.UserDbAdapter= new SqlDataAdapter(); this.UserDbAdapter.DeleteCommand = this.UserDeleteCommand; this.UserDbAdapter.InsertCommand = this.UserInsertCommand; this.UserDbAdapter.SelectCommand = this.UserSelectCommand; this.UserDbAdapter.UpdateCommand = this.UserUpdateCommand; } private bool FillUsers(DataSet UserDataSet, out int […]

重用SqlConnection的最佳实践

我来自Java经验,我试图从C#开始。 我已经阅读了SqlConnection SqlCommand SqlDataReader IDisposable ,我可以理解连接到数据库的最佳做法是在自己的using块中包装SqlConnection , SqlCommand和SqlDataReader 。 但在Java中,我们使用将连接封装到工厂方法中,仅创建一次,并将其重用于所有查询,甚至是multithreading查询。 仅为每个查询创建语句和结果集,并尽快关闭。 是不是为每个查询创建一个新的SqlConnection有点矫枉过正? 不能重复使用吗?

与localhost的SQL连接

我安装DENWER并在内置PhpMyAdmin的帮助下在localhost上创建数据库。 当我在我的C#应用​​程序中连接到数据库时,我得到“SQLexception未处理”。 不明白我的错误在哪里…… 码: SqlConnection connection = new SqlConnection(“Data Source=localhost; User Id=root; Password=; Initial Catalog=MyDB”); connection.Open();

SqlConnection构造函数中无法识别的转义序列

当我连接它时会出错 无法识别的转义序列。 NEPOLEON\ADMN HERE ON THIS SLASH. NEPOLEON\ADMN IS MY SQL SERVER NAME. SqlConnection con = new SqlConnection(“Server=NEPOLEON\ADMN;Database=MASTER;Trusted_Connection=True”);

测试sql连接而不抛出exception

要测试我是否可以连接到我的数据库,我执行以下代码: using (SqlConnection connection = new SqlConnection(myConnectionString)) { try { connection.Open(); canConnect = true; } catch (SqlException) { } } 除非在连接失败时抛出exception,否则此方法有效。 有没有其他方法来测试不抛出exception的Sql连接? 编辑:为了增加精度,我问是否有一个简单的方法可以做到这一点,而无需打开连接并捕获可能发生的exception