views:

249

answers:

6

I'd like to be able to better access the database so I can execute queries (primarily because I don't understand/know the API for it, but I do know SQL). I don't want to remove everything Visual Studio has done because a lot is already built upon it, but how can I get an object that I can use to execute SQL queries.

This is Visual Studio 2008, C#, and MSSQL

+1  A: 

Are you asking what .NET libraries you can use to execute SQL against a database? If so, start by looking at SqlConnection and SqlCommand (if you're using SQL Server, otherwise use OleDbConnection and OleDbCommand). SqlCommand has several Execute() methods that will do what you're looking for.

ADO.NET is a pretty big beast but there are tons of quickstarts and sample code out there.

Robert S.
A: 

I'm not sure from your question what you've got going on, but if you just want to learn how to use a SqlConnection, SqlCommand, and a DataReader object to retrieve items from a database, check this out:

http://msdn.microsoft.com/en-us/library/haa3afyz(VS.71).aspx

routeNpingme
+5  A: 

Try something like this:

using (SqlConnection conn = new SqlConnection("Connection String Goes Here"))
{
    conn.Open();
    using (SqlCommand comm = new SqlCommand("SELECT * FROM TABLE", conn))
    {
        return command.ExecuteScalar() as string;
    }
}

Don't forget to add:

using System.Data;
using System.Data.SqlClient;
Geoffrey Chetwood
Must...resist...urge...to refactor.....
Robert S.
Good, basic rundown, but would have been better to use Db* then Sql* (best practice to use the generic ADO.net classes rather then the SQL Server ones). +1 anyways
Matt Briggs
Go for it. I think it is a nice, easy to understand example though. Don't make it too hard for the newbie.
Geoffrey Chetwood
@Matt: Considering the OP is using MSSQL, it won't be an issue.
Geoffrey Chetwood
I won't even get his head spinning with proper DALs yet...
Geoffrey Chetwood
I'd still like to at see a using statement, try/finally, or even just a note at the bottom mentioning the need to ensure the connection is closed. A quick parameter demonstration would be nice, as well.
Joel Coehoorn
Ah, my wish was (mostly) granted as I typed the comment.
Joel Coehoorn
I aim to please... I was trying to go for more of the tutorial approach, but I guess I shouldn't assume malfist will read up on this.
Geoffrey Chetwood
I do know how to use a database by the way, you know that Rich B, so I know that I need to close the connection too.
Malfist
@Malfist: You have demonstrated to me that you don't know enough, and are not willing to research yourself actually, but I should not assume anything in any post anyway.
Geoffrey Chetwood
This is going to sound so conceited, but why should I do research when people are willing to do it for me for the promise or 'rep'? No seriously, I was just asking a quick question because I was leaving work and was going to check on it when I got home. I'll go through and read up on stuff now.
Malfist
@Malfist: That is an oddly lazy attitude to me, but I don't care either. But I am curious as to how you don't understand how that would make me doubt that you will properly research this.
Geoffrey Chetwood
+1 for the refactor. :) I didn't downvote you btw. This is a great answer.
Robert S.
Why would you ExecuteScalar() a query that selects all columns x all rows?
Constantin
+1  A: 

Not 100% sure what you're really asking for :-), but assuming you're interested in knowing how to programatically execute a SQL query, you'll need (assuming SQL Server as the backend):

  • a "using System.Data;" and "using System.Data.SqlClient;" using clause
  • a "SqlConnection" object to establish your connection to SQL Server (usually combined with a ConnectionString, stored in some form of a config file)
  • a "SqlCommand" object to formulate your SQL query and execute it
  • some way of dealing with possible results from that query

You'll have something like:

SqlConnection connection = new SqlConnection(-connection string-);

SqlCommand command = new SqlCommand("...your sql query here...", connection);

connection.Open();

command.ExecuteScalar / command.ExecuteReader / command.ExecuteNonQuery
(depending on your needs)

connection.Close();

plus of course, some error handling.... :-)

Does that help that at all??

marc_s
+2  A: 

You might also look into the Data Access Application Block - just a DLL you install in your app, which allows you to execute SQL queries and such much more simply:

DataSet ds = SqlHelper.ExecuteDataset(cs,CommandType.StoredProcedure,"SELECT_DATA");
Herb Caudill
+1  A: 

Personally, I would always use the free Ideablade DevForce:

http://www.ideablade.com/

And use no SQL at all!

Chris KL