473,378 Members | 1,518 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Clossing sqlconnection

HI all,

I'm trying my hand at async processing. I have the following, my question is
where do I close the sql connection. Do I close it in this function or in
the ExecCallback function, if so where, after the endexecutereader call?????

Thanks

Robert

string constr2 = ReadConFile("conString.txt".Trim()); //"Data
Source=ROBERT\\SQLExpress;Initial Catalog=Genport;Integrated Security=True;
Connection Timeout = 0;Asynchronous Processing=true";
constr2 += "; Connection Timeout = 0;Asynchronous Processing=true";
SqlConnection con2 = new SqlConnection(constr2);
SqlCommand cmd3 = new SqlCommand();
cmd3.Connection = con2;
cmd3.CommandType = CommandType.Text;
con2.Open();
toolStripStatusLabel1.Text = "Importing Data into table" + ", Please Wait...
";
this.Update();
cmd3.CommandText = cmdstr;
cmd3.CommandTimeout = 0;
AsyncCallback callback = new AsyncCallback(ExecCallback);
cmd3.BeginExecuteReader(callback, cmd3);
Jul 9 '06 #1
2 1604
You close the connection when you are done reading from the DataReader.
If you close the connection before that, the DataReader doesn't work any
more.

Robert Bravery wrote:
HI all,

I'm trying my hand at async processing. I have the following, my question is
where do I close the sql connection. Do I close it in this function or in
the ExecCallback function, if so where, after the endexecutereader call?????

Thanks

Robert

string constr2 = ReadConFile("conString.txt".Trim()); //"Data
Source=ROBERT\\SQLExpress;Initial Catalog=Genport;Integrated Security=True;
Connection Timeout = 0;Asynchronous Processing=true";
constr2 += "; Connection Timeout = 0;Asynchronous Processing=true";
SqlConnection con2 = new SqlConnection(constr2);
SqlCommand cmd3 = new SqlCommand();
cmd3.Connection = con2;
cmd3.CommandType = CommandType.Text;
con2.Open();
toolStripStatusLabel1.Text = "Importing Data into table" + ", Please Wait...
";
this.Update();
cmd3.CommandText = cmdstr;
cmd3.CommandTimeout = 0;
AsyncCallback callback = new AsyncCallback(ExecCallback);
cmd3.BeginExecuteReader(callback, cmd3);

Jul 9 '06 #2
Robert,

You would do it after you cycle through the data reader in the callback
from BeginExecuteReader.

You will want to wrap the call to EndExecuteReader in a try/catch block,
so that if it throws, you can dispose of the connection correctly still (you
can get the connection from the command).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

P.S. You shouldn't be keeping your connection string in a text file like
that. Place it in the config file for the application, as the framework has
support for getting information like this through the classes in the
System.Config namespace.

"Robert Bravery" <me@u.comwrote in message
news:uH**************@TK2MSFTNGP04.phx.gbl...
HI all,

I'm trying my hand at async processing. I have the following, my question
is
where do I close the sql connection. Do I close it in this function or in
the ExecCallback function, if so where, after the endexecutereader
call?????

Thanks

Robert

string constr2 = ReadConFile("conString.txt".Trim()); //"Data
Source=ROBERT\\SQLExpress;Initial Catalog=Genport;Integrated
Security=True;
Connection Timeout = 0;Asynchronous Processing=true";
constr2 += "; Connection Timeout = 0;Asynchronous Processing=true";
SqlConnection con2 = new SqlConnection(constr2);
SqlCommand cmd3 = new SqlCommand();
cmd3.Connection = con2;
cmd3.CommandType = CommandType.Text;
con2.Open();
toolStripStatusLabel1.Text = "Importing Data into table" + ", Please
Wait...
";
this.Update();
cmd3.CommandText = cmdstr;
cmd3.CommandTimeout = 0;
AsyncCallback callback = new AsyncCallback(ExecCallback);
cmd3.BeginExecuteReader(callback, cmd3);


Jul 9 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Donnie Darko | last post by:
I'm trying to understand SqlConnection(), SqlCommand() For example. I use SqlConnection() as a parameter when I create a new SqlCommand. Then I open SqlConnection(), then I execute the...
0
by: Nayt Grochowski | last post by:
Does anyone see any problem with the loading a SqlConnection into the System.Web.HttpContextCurrent.Items collection in a Page's Constructor. Then Closing and Disposing of it the OnUnload method? ...
4
by: The Coolest Dolphin | last post by:
Hi all, I have a question/problem concerning usage of my sqlconnection throughout my program. In my main form (frmMain) I defined an sqlconnection (I've set the connection public so that I can...
17
by: LP | last post by:
Hello, Here's the scenario: Object A opens a Sql Db connection to execute number of SqlCommands. Then it needs to pass this connection to a constructor of object B which in turn executes more...
1
by: Mark | last post by:
I have an SqlConnection object defined as a member of main form. I also have a thread that creates an SqlDataReader but uses the same SqlConnection object. The SqlDataReader simple loops round for...
4
by: Steve Richter | last post by:
I really miss c++ .... I have an SqlConnection object within my Web.UI.Page object. The thinking is that the connection to the database server is opened once when the page starts to do its...
9
by: Roman | last post by:
Hello I have a component built in VB.2003. It has the following property: Public Shared mCONN As SqlConnection Public Property Connection() As SqlConnection Get Return mCONN End Get...
1
by: jinfeng_Wang | last post by:
hi, I have a question about the difference between SqlConnection.IDisposable.Dispose() and SqlConnection.Dispose(). Both of them realize the function of releasing the connection to the...
4
by: Victor | last post by:
Hi Guys I have a problem here. I want to improve the performance for a website. When I looked into the system, I have found that the system made the "SqlConnection Object" static. That mean only...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.