sqlconnection

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

醉酒当歌 提交于 2019-11-30 03:05:17
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 have a drop down list populated with the list of db's they can connect and query. Ideas? You can use SqlConnection.GetSchema : using(var con = new SqlConnection(

Changing SqlConnection timeout

折月煮酒 提交于 2019-11-30 02:33:21
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 = "sproc_StoreData"; command.Parameters.AddWithValue("@TaskPlanID", order.Projects[0].TaskPlanID);

Login failed for user 'sa'. in connection string

五迷三道 提交于 2019-11-29 16:09:47
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;Password=sa;Integrated Security=false"); put your connectionstring in web.config file as I show below

Difference between Sql Connection and OLEDB Connection

↘锁芯ラ 提交于 2019-11-29 10:02:43
What is the difference between the SQL Connection and OLEDB Connection? Is that OLEDB is common to all (also SQL Server)? To which are all Servers, OLEDB is using? The advantage of using OleDbConnection is flexibility. You can change your database (for instance, move to Oracle)and not have to change your code. If you using SQLServer as backend then use SQLConnection for better performance. check with this link http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/fadb2742-d75a-49fb-a839-b2d4a9183998/ OleDbConnection : You can connect to any database, which you have provide for that.

Arithmetic overflow exception when opening SQL connection

醉酒当歌 提交于 2019-11-29 09:22:04
I got very weird ArithmeticOverflowException when opening an SQL connection to the underlying SQL database (stack trace included below). It doesn't make a difference which version of the server is used (I've verified MS SQL 2005/2008/2012/2014), error is still the same. All the newest updates and patches from Windows Update installed. OS is Windows 8.1 / 10 (same occurs on both systems). Server is installed locally and the connection is made via user and password. Connection timeout verified in range from 15 to 1000 sec. The most surprising thing is the application works just fine, and

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

假装没事ソ 提交于 2019-11-29 01:51:02
问题 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

How to write connection string in web.config file and read from it?

狂风中的少年 提交于 2019-11-29 01:46:05
I'm trying to write Connection string to Web.config like this: <connectionStrings> <add name="Dbconnection" connectionString="Server=localhost; Database=OnlineShopping ; Integrated Security=True"/> </connectionStrings > and read from it like this: string strcon = ConfigurationManager.ConnectionStrings["Dbconnection"].ConnectionString; SqlConnection DbConnection = new SqlConnection(strcon); when run the program I get an error because of the null reference. but when I use this code: SqlConnection DbConnection = new SqlConnection(); DbConnection.ConnectionString = "Server=localhost; Database

Will SqlConnection get disposed by GC?

坚强是说给别人听的谎言 提交于 2019-11-28 23:33:16
Disclaimer: I know IDisposable should be implemented when dealing with unmanaged resources. The rest of the code should be deterministic and do using (...) { } (equivalent of try {} finally { Dispose(); } ) to guarantee a cleanup as soon as possible. Also, the GC will not call Dispose() , so the recommended pattern is to override the Finalize() method (in C# using the destructor syntax) which then calls Dispose() . The GC will usually call Finalize() (unless GC.SuppressFinalize() has been called). Problem: So now that I got that out of the way, I have an odd scenario where I cannot do using

Why call SqlClient.SqlDataReader Close() method anyway?

偶尔善良 提交于 2019-11-28 23:19:21
Is the SqlClient.SqlDataReader a .NET managed object or not? Why do we have to call the Close() method explicitly close an open connection? Shouldn't running out of scope for such an object automatically close this? Shouldn't garbage collector clean it up anyway? Please help me understand what is the best practise here. I have seen a related question here and it further illustrates the issue I have with a web application. The issue is that we were running out of connections. The detailed error is here: Exception: System.InvalidOperationException Message: Timeout expired. The timeout period

What is the proper way to ensure a SQL connection is closed when an exception is thrown?

亡梦爱人 提交于 2019-11-28 23:15:40
I use a pattern that looks something like this often. I'm wondering if this is alright or if there is a best practice that I am not applying here. Specifically I'm wondering; in the case that an exception is thrown is the code that I have in the finally block enough to ensure that the connection is closed appropriately? public class SomeDataClass : IDisposable { private SqlConnection _conn; //constructors and methods private DoSomethingWithTheSqlConnection() { //some code excluded for brevity try { using (SqlCommand cmd = new SqlCommand(SqlQuery.CountSomething, _SqlConnection)) {