Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
A question about SqlConnection and scope
Message
From
05/05/2005 18:59:04
 
 
To
05/05/2005 16:33:08
General information
Forum:
ASP.NET
Category:
Databases
Environment versions
Environment:
C# 1.1
OS:
Windows XP SP2
Network:
Windows 2000 Server
Database:
MS SQL Server
Miscellaneous
Thread ID:
01011433
Message ID:
01011468
Views:
14
This message has been marked as the solution to the initial question of the thread.
Hi Alex,

You can return the connection object from your connect method:
public SqlConnection ConnectToSqlServer()
~~Bonnie



>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
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform