• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

.net 2.0 SQL connections

Journer

Banned
So i'm writing a little program to help me out with some stuff at work and i'm developing it in C#. In one part of the program, i need to connect to an MSSQL server, run a procedure, and export the results to csv. i've been doing some searching on how to connect, but everything that comes up is related to LINQ and i cannot use a framework higher than 2.0 otherwise the program will be almost useless.

anyone have any guides on connect to and running SQL queries under the 2.0 framework?
 
LINQ was introduced in 3.5 so it won't be available in 2.0 AFAIK. In .NET 2.0 you would want to use a DataSet, you can point it at your MSSQL server and then access the data in an ORM fashion.
 
yah, LINQ is only availiable in 3.5+. right now i'm reseaching ado.net but can't find anything too helpful. i dont think what i'm doing should be hard...it should be as simple as
create connection class
login to server
run stored procedure
send results through datastream to a file or object
disconnect

eventually i want to add the ability to check if the procedure exists and if not create it...but for now, i just want to get the results
 
SqlDataReader myDataReader = null;

SqlConnection mySqlConnection = new SqlConnection("server=(local)\SQLExpress;Integrated Security=SSPI;database=northwind");
SqlCommand mySqlCommand = new SqlCommand("SELECT EmployeeID, LastName, FirstName, Title, ReportsTo FROM Employees", mySqlConnection);
...
mySqlConnection.Open();
myDataReader = mySqlCommand.ExecuteReader(CommandBehavior.CloseConnection);


well i found that...is there anything different i have to do to run a procedure as opposed to a query?
 
SqlCommand has an overload to accept the name of the stored procedure and the connection object. Before executing the query, set the SqlCommand's CommandyType property to StoredProcedure.

Also, it is advisable to use a DataSet - the DataReader is a forward-read-only object, and always keeps an open connection. Meaning, you're blocking clients on the same connection if it is a long running query. Refer to MSDN.
 
Back
Top