Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
A question about SqlConnection and scope
Message
From
05/05/2005 16:33:08
 
 
To
All
General information
Forum:
ASP.NET
Category:
Databases
Title:
A question about SqlConnection and scope
Environment versions
Environment:
C# 1.1
OS:
Windows XP SP2
Network:
Windows 2000 Server
Database:
MS SQL Server
Miscellaneous
Thread ID:
01011433
Message ID:
01011433
Views:
60
Still in the C# newbie stage, but catching up fast.

I think understand the basics of variable scoping in C#. Now I'm doing the first steps with ADO.NET. I am going to be issuing a lot of "INSERT INTO" statements in a daily batch process, mentioned in a previous thread. I have 99% of the info for the SQL statement already from the ASCII-delimited file. Except for one field that comes from a lookup table.

The sequence (in meta-language) should be:
ConnectToSQLServer()  <-- ideally keep open so I don't have to establish in every record
Read ASCII data line by line
Build SQL Insert statement
Query lookup table for one of the fields (same DB)
Add the new info to the SQL Insert statement (I'm using StrBuilder.Append for every field)
Execute the Insert statement (same DB - ideally same already opened connection)
do it again for next line
I wrote a prototype reading the SqlConnection and SqlCommand samples from help and books and it works fine. But that is creating and closing a connection around every SQL statement. Now I want to put all the parts together and guess that it would be inefficient to be destroying the connection every time.

How do I establish the connection to be in scope?
I originally had a method:
public void ConnectToSqlServer()
that establishes the connection, but it goes out of scope.

Can I create a connection at the Class level?

It most probably is a newbie (naive) question but I'm sure I'll get good advice here. <g>

TIA


Alex Feldstein, MCP, Microsoft MVP
VFP Tips: English - Spanish
Website - Blog - Photo Gallery


"Once again, we come to the Holiday Season, a deeply religious time that each of us observes, in his own way, by going to the mall of his choice." -- Dave Barry
Next
Reply
Map
View

Click here to load this message in the networking platform