sqlconnection

Is it important to Open a sql connection in the transactionscope

人走茶凉 提交于 2020-01-13 08:57:09
问题 I created a sqlconnection, CN1. Then this CN1 is opened. Later in the code there is a transactionscope. If I execute a sql command on this CN1 connection, is this within transaction? Code looks like this; SqlConnection cn1 = new SqlConnection(); cn1.Open(); //connection opened when there is no ambient transaction. ... using(TransactionScope scope = new TransactionScope()) { SqlCommand cmd; //a typical sql command. ... cmd.ExecuteNonQuery(); //Is this command within transaction? ... } 回答1: It

Remote connection to SQL Server Express fails

♀尐吖头ヾ 提交于 2020-01-06 02:05:20
问题 I have two computers that share the same Internet IP address. Using one of the computers, I can remotely connect to a SQL Server database on the other. Here is my connection string: SqlConnection connection = new SqlConnection(@"Data Source=192.168.1.101\SQLEXPRESSNI,1433;Network Library=DBMSSOCN;Initial Catalog=FirstDB;Persist Security Info=True;User ID=username;Password=password;"); 192.168.1.101 is the server, SQLEXPRESSNI is the SQL Server instance name, and FirstDB is the name of the

Remote connection to SQL Server Express fails

余生长醉 提交于 2020-01-06 02:04:06
问题 I have two computers that share the same Internet IP address. Using one of the computers, I can remotely connect to a SQL Server database on the other. Here is my connection string: SqlConnection connection = new SqlConnection(@"Data Source=192.168.1.101\SQLEXPRESSNI,1433;Network Library=DBMSSOCN;Initial Catalog=FirstDB;Persist Security Info=True;User ID=username;Password=password;"); 192.168.1.101 is the server, SQLEXPRESSNI is the SQL Server instance name, and FirstDB is the name of the

How secure the user name and password in the connection string?

此生再无相见时 提交于 2020-01-02 03:26:25
问题 when developing windows applications: How I secure the user name and password in the connection string? Organizations like banks, would they give out the user name and password of their DB to application developers? if not typically how those applications developers write the DB Connections? What is the industry standard to secure user and password in the connection string? thanks 回答1: How I secure the user name and password in the connection string? Either use Windows authentication to

Test sql connection without throwing exception

怎甘沉沦 提交于 2020-01-02 02:19:05
问题 To test if i can connect to my database, I execute the following code : using (SqlConnection connection = new SqlConnection(myConnectionString)) { try { connection.Open(); canConnect = true; } catch (SqlException) { } } This works except it throws an exception if the connection failed. Is there any other way to test a Sql connection that doesn't throw an exception ? Edit : To add precision, i'm asking if there is a simple method that does that without having to open the connection and catch

EntityConnection and Opened SqlConnection

不问归期 提交于 2020-01-01 12:43:09
问题 I've a question about EntityFramework with CodeFirst approach. Based on EntityConnection source code and documentation I can't create it with already opened SqlConnection. It requires that is should be closed. We have some different DB data layers(nHibernate,etc), and it would be good, if we can reuse the same SqlConnection between them. But seems, that EntityFramework doesn't allow to do this. I can pass closed SqlConnection to DbContext, but it isn't good for me to close connection for each

SqlConnection Thread-Safe?

喜你入骨 提交于 2020-01-01 05:09:09
问题 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

Connection.open for hangs indefinitely, no exception is thrown

萝らか妹 提交于 2019-12-30 06:01:08
问题 When I try to do the following code, the program hangs indefinitely. I don't know why and there seems to be other unanswered topics on the matter. Although, if the IP\website cannot be reached, then it works as intended. private void DoStuff() { string connectionString = "Data Source=www.google.com;Connection Timeout=5"; using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); //Hangs here indefinitely Console.WriteLine("Test"); } } For example, if I set the

Login failed for user 'sa'. in connection string

早过忘川 提交于 2019-12-29 09:06:21
问题 I'm getting the following error: Login failed for user 'sa' When I try to connect server by setting value through a string variable: private SqlConnection getDbConnection = new SqlConnection("Data Source="+dbname+";Initial Catalog="+catname+";User Id=sa;Password=sa;Integrated Security=false"); But when I use the normal connection string with no string variable it works well: private SqlConnection getDbConnection = new SqlConnection("Data Source=Ali-pc\\;Initial Catalog=master;User Id=sa

Check if SQL Connection is Open or Closed

半城伤御伤魂 提交于 2019-12-28 09:32:57
问题 How do you check if it is open or closed I was using if (SQLOperator.SQLCONNECTION.State.Equals("Open")) however, even the State is 'Open' it fails on this check. 回答1: You should be using SqlConnection.State e.g, using System.Data; if (myConnection != null && myConnection.State == ConnectionState.Closed) { // do something // ... } 回答2: Here is what I'm using: if (mySQLConnection.State != ConnectionState.Open) { mySQLConnection.Close(); mySQLConnection.Open(); } The reason I'm not simply using