sqlconnection

C# DbConnection cast to SqlConnection

有些话、适合烂在心里 提交于 2019-12-01 03:00:07
I found this piece of code in one application Database database = DatabaseFactory.CreateDatabase("connection string"); DbConnection connection = database.CreateConnection(); connection.Open(); SqlConnection sqlConnection = (SqlConnection)connection; Is it safe, SqlConnection derieve from DbConnection. Database comes from Microsoft.Practices.EnterpriseLibrary.Data. According to documentation CreteDatabase returns DbConnection. No it is not safe, casting is never safe and it may blow anytime while your application is running. While SqlConnection derives indeed from DbConnection you are not

Call dll function from sql stored procedure using the current connection

萝らか妹 提交于 2019-12-01 01:49:57
Can I call a dll from a stored procedure using the open connection? I have a dll that gets data from SQL Server and I don't want to open a new connection when I call it from the stored procedure. Thank you Here is an exemple public class Class1 { public static SqlString GetName(SqlString str) { SqlCommand cmd = new SqlCommand(str.ToString()); cmd.CommandType = System.Data.CommandType.Text; string name = cmd.ExecuteScalar().ToString(); return name; } } and this is the SQL code CREATE FUNCTION fn_TestConnection ( @str nvarchar(255) ) RETURNS nvarchar(max) AS EXTERNAL NAME TestConnection.

Most efficient way to test SQL connection string availibility

最后都变了- 提交于 2019-11-30 23:21:05
问题 I have this code down which I tried to make it Test SQL string connectivity, but I dont know how to handle the part with connection.Open = true would you please help me to solve this out? Thank you so much for your time. private void button1_Click(object sender, EventArgs e) { try { using (SqlConnection connection = new SqlConnection("Data Source='" + textBox1.Text + "';Initial Catalog='" + textBox2.Text + "';User ID='" + textBox3.Text + "';Password='" + textBox4.Text + "'")) { try {

Why do we need to set Min pool size in ConnectionString

时光怂恿深爱的人放手 提交于 2019-11-30 17:49:15
For SQL connection pool, why do we need to set up a min pool size? As connections will be saved in the connection pool and reused, why do we need to keep live connections specified by the min pool size? Thanks. Opening and maintaining connections is expensive, so if you know that you need multiple connections (always) it's better to specify the MinPoolSize because then it's ensured that these connections are available. Also, from MSDN : If MinPoolSize is either not specified in the connection string or is specified as zero, the connections in the pool will be closed after a period of

Why do we need to set Min pool size in ConnectionString

断了今生、忘了曾经 提交于 2019-11-30 16:43:02
问题 For SQL connection pool, why do we need to set up a min pool size? As connections will be saved in the connection pool and reused, why do we need to keep live connections specified by the min pool size? Thanks. 回答1: Opening and maintaining connections is expensive, so if you know that you need multiple connections (always) it's better to specify the MinPoolSize because then it's ensured that these connections are available. Also, from MSDN: If MinPoolSize is either not specified in the

“Login failed for user” C# with SQLConnection

北战南征 提交于 2019-11-30 14:56:04
问题 I've been trying to connect to my database (which is on the same computer as my code) through my C# code. The problem is I keep getting the "Login failed for user " "" error... I admit that my knowledge of connecting to databases is minimal and I've tried almost all the steps in other questions! here's part of my code: SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLServerConnection"].ConnectionString); SqlCommand command = connection.CreateCommand();

“Login failed for user” C# with SQLConnection

拜拜、爱过 提交于 2019-11-30 12:44:55
I've been trying to connect to my database (which is on the same computer as my code) through my C# code. The problem is I keep getting the "Login failed for user " "" error... I admit that my knowledge of connecting to databases is minimal and I've tried almost all the steps in other questions! here's part of my code: SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLServerConnection"].ConnectionString); SqlCommand command = connection.CreateCommand(); command.CommandText = @"IF EXISTS ( SELECT * FROM user WHERE EMAILADRES = @email and WACHTWOORD =

C# connect to database and list the databases [duplicate]

扶醉桌前 提交于 2019-11-30 12:35:04
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: SQL Server query to find all current database names I am trying to figure out how to list the databases after connecting to the servers without specifying a database first. sqlConnection1 = new SqlConnection("Server=" + sqlServer + ";Database=" + database + ";User ID=" + userName + ";Password=" + password + ";Trusted_Connection=False;"); So basically what i want is the end user to connect to the sql server, then

Changing SqlConnection timeout

本小妞迷上赌 提交于 2019-11-30 12:22:32
问题 I am trying to override the default SqlConnection timeout of 15 seconds and am getting an error saying that the property or indexer cannot be assigned because it is read only. Is there a way around this? using (SqlConnection connection = new SqlConnection(Database.EstimatorConnection)) { connection.Open(); using (SqlCommand command = connection.CreateCommand()) { command.CommandType = CommandType.StoredProcedure; connection.ConnectionTimeout = 180; // This is not working command.CommandText =

Should I be using SqlDataReader inside a “using” statement?

一曲冷凌霜 提交于 2019-11-30 04:38:31
Which of the following two examples are correct? (Or which one is better and should I use) In the MSDN I found this: private static void ReadOrderData(string connectionString) { string queryString = "SELECT OrderID, CustomerID FROM dbo.Orders;" using (SqlConnection connection = new SqlConnection(connectionString)) { SqlCommand command = new SqlCommand(queryString, connection); connection.Open(); SqlDataReader reader = command.ExecuteReader(); // Call Read before accessing data. while (reader.Read()) { Console.WriteLine(String.Format("{0}, {1}", reader[0], reader[1])); } // Call Close when done