LoneXcoder
LoneXcoder

Reputation: 2163

sql database interaction from visual studio

I have a helper class in my root AppCode folder that saves me time and coding executing database related tasks

and i am looking for a way to have all tables and columns names into the intelliSense

so for example

public static class GetTableData
{
    //base method
    static int ExecSQLint(string TblintSQL)
    {
        int value;
        SqlConnection myConn = new SqlConnection("server=(local);Initial Catalog=dbName;Integrated Security=True");
        SqlCommand TblintCMD = new SqlCommand(TblintSQL, myConn);

        try
        {
            myConn.Open();
            value = Convert.ToInt32(TblintCMD.ExecuteScalar());
        }
        finally
        {
            myConn.Close();
        }

        return value;
    }

    //return a Database value as int 
    public static int AsInt(string FieldToSearch, string tblName, string WhereClause, string FLdValue)
    {
        string TblintSQL = "SELECT " + FieldToSearch + " FROM " + tblName + " WHERE " + WhereClause + " ='" + FLdValue + "'";
        return ExecSQLint(TblintSQL);
    }
}

so if i need a customer id(int) from a table for example

some [tblCustomers] i have a column named custID and name

i will retrive customer id via following Code

int custId = GetTableData.AsInt("custID", "tblCustomers", "name", "warren");

I would like to get available Columns and table names into Visual studio so i will not have to use strings as used in example above what would be the simplest way to achieve that ?,

is it by generating a code that will generate a class with const strings

or is there a utility I don't know about inside the Visual Studio ? (I'm currently using 2010 Pro)

Upvotes: 0

Views: 332

Answers (1)

AntLaC
AntLaC

Reputation: 1225

You can use Entity Framework to build classes from your tables, views and stored procedures.

http://msdn.microsoft.com/en-us/library/bb386876(v=vs.100).aspx

Upvotes: 2

Related Questions