Close database connection on SQL Server using ADO .NET - sql-server

I'm connecting to database using ADO .NET:
Dim conn As SqlConnection
Dim sqlcmd As SqlCommand
Dim da As SqlClient.SqlDataAdapter
Dim table As DataTable
conn = New SqlConnection(Utilities.ConnectionString)
sqlcmd = New SqlClient.SqlCommand()
sqlcmd.Connection = conn
sqlcmd.CommandType = CommandType.StoredProcedure
sqlcmd.CommandText = "mySP"
sqlcmd.Parameters.Add(New SqlClient.SqlParameter("#param", param1))
da = New SqlClient.SqlDataAdapter()
da.SelectCommand = sqlcmd
table = New DataTable()
da.Fill(table)
conn.Close()
sqlcmd.Connection.Close()
That works good.
When I launch on SQL Server the command:
EXEC SP_WHO2
For each call made from the previous code in ADO .NET, I have in the field Command the value: "AWAITING COMMAND", and in the field Status the value is "sleeping".
What does this means? The connection to database is still active? What should I do in order to close db connection?

The fact that after few hours you receive errors about connections exausted means that somewhere in your code you don't dispose correctly of your connection.
The code above seems correct, but what happen if you have exceptions? Are you sure to handle correctly the situation when your code exits unexpectedly from a method, due to exceptions?
The correct approach to this situation is refactoring your code.
Introduce everywhere the Using pattern. So your code above become:
Dim conn As SqlConnection
Dim sqlcmd As SqlCommand
Dim da As SqlClient.SqlDataAdapter
Dim table As DataTable
Using conn = New SqlConnection(Utilities.ConnectionString)
Using sqlcmd = New SqlClient.SqlCommand()
sqlcmd.Connection = conn
sqlcmd.CommandType = CommandType.StoredProcedure
sqlcmd.CommandText = "mySP"
sqlcmd.Parameters.Add(New SqlClient.SqlParameter("#param", param1))
da = New SqlClient.SqlDataAdapter()
da.SelectCommand = sqlcmd
table = New DataTable()
da.Fill(table)
End Using
End Using
This approach will ensure that your disposable objects (connection and command) will be released correctly and you will remove the subtle problem of connection leakings.

Related

Using SqlBulkCopy with 2 Different Connection Strings

I have an application that gets information from one server (server config stuff) and puts it into a centralized database on another server. There are several servers that I loop over from a text file. I'm writing this application to eliminate the need for linked servers.
I'm trying to use SqlBulkCopy for copying the data but I'm running into problems. I have been successful using SqlBulkCopy with a single SQL connection but when I try to use 2 different SQL connections I run into problems. The data never reaches its destination database. I have debugged through the code and can see that it reaches the try/catch but it appears that the command bulkcopy.WriteToServer(reader) it doesn't actually write any data to SQL.
Dim connectionString As String = "Server= <ThatServer>; integrated security=true"
Dim connectionString2 As String = "Server= <MyServer>; Initial Catalog = <CentralizedDatabase>; integrated security=true"
Using sourceConnection As SqlConnection = _
New SqlConnection(connectionString)
sourceConnection.Open()
Dim commandSourceData As SqlCommand = New SqlCommand("<mySQLcommand>", sourceConnection)
Dim reader As SqlDataReader = commandSourceData.ExecuteReader
Using sourceConnection2 As SqlConnection = _
New SqlConnection(connectionString2)
sourceConnection2.Open()
Using destinationConnection As SqlConnection = _
New SqlConnection(connectionString2)
Using bulkcopy As SqlBulkCopy = _
New SqlBulkCopy(destinationConnection)
bulkcopy.DestinationTableName = _
"<MyTableName>"
Try
bulkcopy.WriteToServer(reader)
Catch ex As Exception
Console.WriteLine(ex.Message)
Finally
reader.Close()
End Try
End Using
End Using
sourceConnection2.Close()
End Using
sourceConnection.Close()
End Using
Can anyone see what I'm doing wrong? Thanks in advance!
**Solved. Here's my code that works:
Dim connectionString As String = "Server = <ThatServer>; Initial Catalog = <sourceDB>; integrated security = true"
Dim connectionString2 As String = "Server= <MyServer>; Initial Catalog = <destinationDB>; integrated security=true"
Using sourceConnection As SqlConnection = _
New SqlConnection(connectionString)
Dim commandSourceData As SqlCommand = New SqlCommand(<TSQL>, sourceConnection)
sourceConnection.Open()
Dim reader As SqlDataReader = commandSourceData.ExecuteReader()
Using destinationConnection As SqlConnection = _
New SqlConnection(connectionString2)
destinationConnection.Open()
Using bulkCopy As SqlBulkCopy = _
New SqlBulkCopy(destinationConnection)
bulkCopy.ColumnMappings.Add("SourceColumn1", "DestinationColumn1")
bulkCopy.ColumnMappings.Add("SourceColumn2", "DestinationColumn2")
bulkCopy.ColumnMappings.Add("SourceColumn3", "DestinationColumn3")
bulkCopy.ColumnMappings.Add("SourceColumn4", "DestinationColumn4")
bulkCopy.ColumnMappings.Add("SourceColumn5", "DestinationColumn5")
bulkCopy.ColumnMappings.Add("SourceColumn6", "DestinationColumn6")
bulkCopy.ColumnMappings.Add("SourceColumn7", "DestinationColumn7")
bulkCopy.DestinationTableName = "<destinationTable>"
bulkCopy.WriteToServer(reader)
End Using
destinationConnection.Close()
End Using
reader.Close()
sourceConnection.Close()
End Using

MS Access to Execute Proc in SQL Server

I have a Proc for creating and updating a table in SQL Sever. The result table is linked to the MS ACCESS FE. It was before created by Macro involving queries in MS Access. How could I call or execute the Proc from MS Access FE.
I tried this code but it doesn't result in anything.
Dim cmd As ADODB.Command
Set cmd = New ADODB.Command
cmd.ActiveConnection = "Provider=SQLNCLI11.1;Security=SSPI;Initial Catalog=MYDB;Server=MyServer;Integrated Security=SSPI;"
cmd.CommandType = adCmdStoredProc
cmd.CommandText = "UPDATE_DATA"
cmd.Execute
I'm new to VBA Coding. Any help or leads will be greatly appreciated.
In your code, you haven't opened a connection to the database. Open a connection first like in the below code:
Private Const gcConn As String = "Provider=SQLNCLI11.1;Security=SSPI;Initial Catalog=MYDB;Server=MyServer;Integrated Security=SSPI;"
Sub UpdateProc()
Dim oDB As ADODB.Connection
Dim oCM As ADODB.Command
Set oDB = New ADODB.Connection
oDB.Open gcConn
Set oCM = New ADODB.Command
With oCM
.ActiveConnection = oDB
.CommandType = adCmdStoredProc
.CommandText = "UPDATE_DATA"
.Execute
End With
oDB.Close
Set oDB = Nothing
End Sub
Simply save your command as a pass-though query. Thus your query is:
UPDATE_DATA
Then this one line of code will run that query
CurrentDb.QueryDefs("MyPass").Execute

Long running SP never ends using sqlCommand

I've got a quite expensive stored procedure in my SQL Server database. Launching it from the SQL Server Managment Studio requires some minutes. But I can't launch it via code using SqlCommand.
I've got this code:
spExecQuery = "EXEC [schema].[storedName]"
If I use this vb.NET snippet:
Using sqlCmd As SqlCommand = New SqlCommand(spExecQuery, conn)
sqlCmd.ExecuteNonQuery()
End Using
The script ends with "Timeout expired" error. But if I do:
Using sqlCmd As SqlCommand = New SqlCommand(spExecQuery, conn)
sqlCmd.CommandTimeout = 0
sqlCmd.ExecuteNonQuery()
End Using
The script never ends (it's running from at least 2 hours)... What am I missing? Thank you.
You need to specify first that it's a stored-procedure:
Using sqlCmd As SqlCommand = New SqlCommand(spExecQuery, conn)
sqlCmd.CommandType = CommandType.StoredProcedure
sqlCmd.CommandTimeout = 0
sqlCmd.ExecuteNonQuery()
End Using
You could also use QueueUserWorkItem to let the method run asynchronously, then you don't need to wait until it has finished:
Public Shared Sub ExecuteStoredProcedureAsync()
Threading.ThreadPool.QueueUserWorkItem(
New Threading.WaitCallback(AddressOf ExecuteStoredProcedure)
)
End Sub
Private Shared Sub ExecuteStoredProcedure(threadState As Object)
Dim sw = New Stopwatch()
sw.Start()
Try
' add code or call of long running method here '
Log.WriteInfo(String.Format("ExecuteStoredProcedure(async call) executed successfully, execution-time: {0}.", sw.Elapsed))
Catch ex As Exception
Log.WriteError(String.Format("Exception in ExecuteStoredProcedure (async call), execution-time: {0}.", sw.Elapsed))
End Try
End Sub
It should work if you specify the CmdType.
If still not working, try using the SqlDataReader
cmd.Connection = conn
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "proc_name"
Dim dr As SqlDataReader = cmd.ExecuteReader
try to add the command type to your sqlcommand
sqlCmd.CommandType = CommandType.StoredProcedure

retrieving data in VB from SQL

I use Visual Basic 2010 and Microsoft SQL Server 2008. I have my database and my table and i made the connection (at least i think i did) in VB using only the interface.
What i want to know is how to get data from the database and use it into my VB project. I have of course searched for solutions already but the differences i find only confuse me more. What i need to know are the basics, the tools/objects and procedures to retrieve the data.
What i try to do at the moment is make a simple selection and put that data into a listbox right when the program starts, like this:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
SqlConnection1.Open()
SqlConnection1.Close()
End Sub
End Class
1) Create your connection string
Dim connectionString As String = "Data Source=localhost;........."
2) Connect to your Database
Dim connection As New SqlConnection(connectionString)
conn.Open()
3) Create a Command and the query
Dim command As New SqlCommand("SELECT * FROM Product", connection)
Dim reader As SqlDataReader = command.ExecuteReader() //Execute the Query
4) Retrieve your result. There are several ways
Dim dt As New DataTable()
dt.Load(reader)
'Close the connection
connection.Close()
5) Bind to your list box
myListBox.ItemSource = dt
Full code here
Using connection As New SqlConnection(connectionString)
Dim command As New SqlCommand("Select * from Products", connection)
command.Connection.Open()
SqlDataReader reader = command.ExecuteReader()
End Using
For more info
SQLCommand
SqlConnection1.Open()
using table As DataTable = New DataTable
using command as SqlCommand = New SqlCommand("SELECT blah blah", SqlConnection1)
using adapter As SqlDataAdapter = new SqlDataAdapter(command)
adapter.Fill(table)
end using
end using
for each row As DataRow in table.Rows
' add each listbox item
listbox1.Items.Add(row("column name"))
next
end using
SqlConnection1.Close()

How to programmatically attach with server instance through vb.net

I need to attach my database, that resided in the MyData directory of my application folder, to the instance of SQL server on client's machine. How to do it?
Could any body give me vb.net code for that so my application does it when run for the first time?
Someone suggested me
Dim cmd As New SqlCommand()
' Dim vrMyConString As String=
Dim conn As System.Data.SqlClient.SqlConnection ' = New SqlConnection(vrMyConString)
cmd.CommandText = "sp_attach_db 'e:\dbTest.mdf', 'e:\dbTest.ldf'"
'conn.connectionstring =
conn.open()
cmd.CommandType = CommandType.StoredProcedure
cmd.Connection = conn
cmd.executenonquery()
but on conn.open, it returns error, Object reference not set to an instance of object
Thanks
This works on my Vb.net application.
Make sure sure your connection string: vrMyConString is correct and pointing to your database and with the correct details and it should work
Dim conn As SqlConnection = Nothing
conn = New SqlConnection(vrMyConString )
conn.Open()
You may use ADO.NET provider Linq to SQL or Entity Framework API.

Resources