Search code examples
c#sqlsql-servervisual-studio-2008devforce

Going from VisualStudio generated Database stuff to Programmer Generated


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


Solution

  • 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;