Click here to Skip to main content
15,907,326 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Dear all,

in my project I'm dealing with Microsoft SQL Server database.
I know one way of retrieving data from it from my c# code:

SqlCommand myCommand = new SqlCommand("select something from someTable where someCondition", myConnection);
     SqlDataReader myReader = myCommand.ExecuteReader();


and data is now in myReader object. That's fine.

But making query is much easier, for me anyway, by creating Database Query and saving it in *.dtq file.
So my question would be, whether it's possible to call query which is in *.dtq file from C# code in a similar fashion as above, and if it is, how to do it.

Thanks in advance.
Posted
Updated 21-Jan-11 6:37am
v2

A common way of doing this is via stored procedures in the SQL server database. You can then invoke the stored procedures and pass it any required arguments.

And as for your original question, no, I don't think using dtq files is a very popular approach. :~
 
Share this answer
 
v2
Comments
fjdiewornncalwe 24-Jan-11 14:51pm    
+5. Agreed. If the OP wants to separate his sql code from his source (A great idea), then procedures are probably the best and most used way to go.
Ok, thanks. I see error of my ways now.

After Nishant Sivakumar suggested stored procedures as solution for the problem, I did some reading on the internet, so if you have the same problem as I did (once upon the time) and you are used to C# and/or VB but know only vary basics of databases and SQL, here is what I think to be usefull for you to read:

http://databases.about.com/od/sqlserver/a/storedprocedure.htm[^]
to get the basic idea what is stored procedure and why to use it, and not query, than

http://www.sql-server-performance.com/articles/dba/stored_procedures_basics_p1.aspx[^]
to cover the topic in more detail, and finaly

http://www.csharpfriends.com/articles/getarticle.aspx?articleid=78[^]
to learn how to call your stored procedures from C# code.

Thanks again for help.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900