sqlconnection

SqlConnection Thread-Safe?

与世无争的帅哥 提交于 2019-12-03 14:19:13
I have a Log class which put logs in Windows journal and in a SQL table. In order to optimize my code, I would like use only one SqlConnection . In MSDN, it says: Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe. My question is : private static readonly SqlConnection conn = new SqlConnection(ConfigParameters.Instance.UIDConnection); Is it thread-safe ? If yes, when use Open() and Close() ? If no, how use properly SqlConnection ? Here is my full class code : private static readonly SqlConnection conn = new

.net SqlConnection not being closed even when within a using { }

让人想犯罪 __ 提交于 2019-12-03 10:15:13
Please help! Background info I have a WPF application which accesses a SQL Server 2005 database. The database is running locally on the machine the application is running on. Everywhere I use the Linq DataContext I use a using { } statement, and pass in a result of a function which returns a SqlConnection object which has been opened and had an SqlCommand executed using it before returning to the DataContext constructor.. I.e. // In the application code using (DataContext db = new DataContext(GetConnection())) { ... Code } where getConnection looks like this (I've stripped out the 'fluff' from

How to use the ConfigurationManager.AppSettings

℡╲_俬逩灬. 提交于 2019-12-03 03:01:44
问题 I've never used the "appSettings" before. How do you configure this in C# to use with a SqlConnection, this is what I use for the "ConnectionStrings" SqlConnection con = new SqlConnection(); con.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; And this is what I have for the "appSettings" SqlConnection con = new SqlConnection(); con = ConfigurationManager.AppSettings("ConnectionString"); but it is not working. 回答1: ConfigurationManager

SqlConnection - is remote or local connection?

谁都会走 提交于 2019-12-02 18:35:06
问题 How can I determine is it local connection (localhost or 127.0.0.1) or is it remote connection (other machine in local area) if I have SqlConnection object? 回答1: Easiest way that I am aware of is to check the connectionstring directly to see if it contains either the words localhost, 127.0.0.1, (localhost), "." or the local machine name. Check for starting with since their could be a local named instance of Sql running. You can use the System.Environment library to retrieve the current

Why is my table not being created?

∥☆過路亽.° 提交于 2019-12-02 17:21:30
问题 I've got this code in my Winforms app to create a table in an existing database, (which I created by following what is written here): private void CreateTables() { string connStr = @"Data Source= (LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory| \AYttFM.mdf;Integrated Security=True"; using (var connection = new System.Data.SqlClient.SqlConnection(connStr)) { try { connection.Open(); using (var command = connection.CreateCommand()) { StringBuilder sb = new StringBuilder(); sb.Append(

How to use the ConfigurationManager.AppSettings

妖精的绣舞 提交于 2019-12-02 16:34:59
I've never used the "appSettings" before. How do you configure this in C# to use with a SqlConnection, this is what I use for the "ConnectionStrings" SqlConnection con = new SqlConnection(); con.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; And this is what I have for the "appSettings" SqlConnection con = new SqlConnection(); con = ConfigurationManager.AppSettings("ConnectionString"); but it is not working. Anton Vidishchev ConfigurationManager.AppSettings is actually a property, so you need to use square brackets. Overall, here's what you need

Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding [closed]

一笑奈何 提交于 2019-12-02 12:58:06
hi I'm getting an error Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding . I'm alredy changed the connect timeout = 60000 and in database my procedure is executes in 43sec. so plz give me some perfect solution thank you You should note that Timeout property for SqlConnection object and Timeout property for SqlCommand object are different properties. By default command timeout is set to 30 seconds. Set it to 60, and your issue will be solved: commandObject.Timeout = 60; But most likely that you should change your SQL procedure or

Unable to load 'Microsoft.Data.ConnectionUI" for DataConnectionDialog use

对着背影说爱祢 提交于 2019-12-02 12:19:43
问题 I created an application in C# , .Net 4.0. In this application i need to show a DataConnectionDialog for accessing DataBase. I use this code : DataConnectionDialog BDdialog = new DataConnectionDialog(); DataSource.AddStandardDataSources(BDdialog); BDdialog.SelectedDataSource = DataSource.OdbcDataSource; BDdialog.SelectedDataProvider = DataProvider.OdbcDataProvider; DataConnectionDialog.Show(BDdialog); On my laptop, the code works fine, but on another laptot, with .Net 4.0 and .Net 4.5 there

Passing around a SqlConnection

梦想与她 提交于 2019-12-02 09:59:03
问题 I have created a TransactionScope and within the scope various items are created and updated in the database. During this process I make a lot of calls to the database. Originally I opened a SqlConnection in the beginning of the TransactionScope and passed it around to any function that made a DB call then I closed the connection after all the calls are made and before the transaction commits. Is it better to do this or to open and close a connection (using the same connection string) for

Is it necessary to dispose SqlConnection as well as SqlCommand?

微笑、不失礼 提交于 2019-12-02 08:52:07
问题 Is it necessary to separately dispose of a SqlConnection as well as a SqlCommand? I'm doing some maintenance work on an application and found the following code: public void CallStoredProc(string storedProcName) { using (SqlCommand command = new SqlCommand(storedProcName, CreateConnection())) { command.CommandType = CommandType.StoredProcedure; command.Connection.Open(); // Additional code here. command.ExecuteNonQuery(); } } Will this clean up the SqlConnection properly, under normal