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