sqlconnection

Why is my table not being created?

强颜欢笑 提交于 2019-12-02 08:32:02
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("CREATE TABLE [dbo].[AssignmentHistory] "); sb.Append("("); sb.Append("[Id] INT NOT NULL PRIMARY KEY, ");

Is it necessary to dispose SqlConnection as well as SqlCommand?

北城以北 提交于 2019-12-02 07:12:48
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 circumstances as well as if there is an error? Or would we have to alter the code to something like: public

ClassNotFoundException when trying to connect to SQL server 2005 with Java

这一生的挚爱 提交于 2019-12-02 06:54:17
问题 I'm fairly new to database management. I'm just trying to connect to the database and retrieve and display a table in the command prompt. The database is not on my computer. I am fairly certain that the url is the problem. The code: import java.io.*; import java.sql.*; class transfer{ //driver and DB URLs final static String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; final static String DB_SQL = "jdbc:microsoft:sqlserver://localhost:1433;" + "database=DataDB;" + "user=sa;" +

Passing around a SqlConnection

本小妞迷上赌 提交于 2019-12-02 05:44:45
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 each call? If you're going to make a lot of calls in a row, and it's easy to pass in an open connection,

in c# global connection to be used in all classes

本小妞迷上赌 提交于 2019-12-02 05:43:15
问题 I've a connection string saved to Properties.Settings.Default , i need to use an sql connection in all classes without having to declare it everytime, so how should it be declared? 回答1: You actually don't need to use a single SqlConnection everywhere. A better approach would be to create a class to manage you data access. Typically this is the duty of your Data Access Layer (DAL). DAL is a set of classes that handle all the database related stuff. A very simple class for this purpose could be

Can a .NET SqlConnection object cause a memory leak if it is not closed?

人盡茶涼 提交于 2019-12-02 02:38:58
问题 I understand that you need to call .Close() on a SqlConnection object to release the underlying SQL connection back to the pool when you are done with it; but if you refrain from doing so, does the .NET object remain in memory even after going out of scope? I ask because I am working with some code that is experiencing memory leaks and I noticed that the SqlConnection objects are not being closed or disposed (they are created, opened, then simply allowed to go out of scope). 回答1: The issue

in c# global connection to be used in all classes

China☆狼群 提交于 2019-12-01 23:18:06
I've a connection string saved to Properties.Settings.Default , i need to use an sql connection in all classes without having to declare it everytime, so how should it be declared? You actually don't need to use a single SqlConnection everywhere. A better approach would be to create a class to manage you data access. Typically this is the duty of your Data Access Layer (DAL). DAL is a set of classes that handle all the database related stuff. A very simple class for this purpose could be something like this: public class DatabaseManager { private static DatabaseManager _instance; private

What is the relationship between open SqlConnections in the client app and processes in SQL Server?

╄→尐↘猪︶ㄣ 提交于 2019-12-01 22:14:32
I just tried to make a simple schema change to a table in a SQL Server database (by using the Design tool in SMSS). Whenever I tried to save the change, it kept timing out. I wondered whether this was due to existing connnections which were 'locking' the table. I decided to kill connections as an experiment. I queried master..sysprocesses to get the current spids for that database, and killed them one by one until I was able to save my schema change. (Not very scientific, but I'm far from an expert with SQL Server). Sure enough, when I had killed all the spids (bar the one which was me using

MySQL “network-related or instance-specific error occurred while establishing a connection to SQL Server”

让人想犯罪 __ 提交于 2019-12-01 21:22:22
问题 I've been looking around for a solution to this issue and the only one that might provide a solution is the only one I haven't tried, because it involves changing system properties. I want to avoid that if I can. The Connection string is correct, but it still won't connect. The exception occurs on line 30, which is where the connection string is opened. conn.Open() Here is the Code: using System.Text; using System.Data.SqlClient; namespace My_Sql_Program { class Program { static void Main() {

Is closing/disposing an SqlDataReader needed if you are already closing the SqlConnection?

筅森魡賤 提交于 2019-12-01 21:03:56
I noticed This question , but my question is a bit more specific. Is there any advantage to using using (SqlConnection conn = new SqlConnection(conStr)) { using (SqlCommand command = new SqlCommand()) { // dostuff } } instead of using (SqlConnection conn = new SqlConnection(conStr)) { SqlCommand command = new SqlCommand(); // dostuff } Obviously it does matter if you plan to run more than one command with the same connection, since closing an SqlDataReader is more efficient than closing and reopening a connection (calling conn.Close();conn.Open(); will also free up the connection). I see many