2

Is there any way to retrive data from Database with out writing any sql query . I mean i want to read data into my label fileds with out writing any query in sqlcommand.Please anyone help me or tell me how can i do this in c# or vb.net

Update

 protected void Page_Load(object sender, System.EventArgs e)
    {
        string connectionString = @"Data Source=Data Source=.\SQLEXPRESS;AttachDbFilename=E:\Test\Database.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
         SqlConnection con = new SqlConnection(connectionString);        
        con.Open();
        SqlCommand cmd = new SqlCommand("Select * from Test", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds, "Test");
        GridView1.DataSource = ds;
        GridView1.DataBind();        
        con.Close();
    }
3
  • using LINQ is also similar to querying the database. To get the data from database we need to send the request. and that request is your select statement which will give you results as per the query. Commented Feb 24, 2012 at 13:01
  • 2
    Jedi Magic... summon your inner force. Commented Feb 24, 2012 at 13:53
  • Can you use a stored procedure? Commented Feb 24, 2012 at 13:54

6 Answers 6

3

If you don't want to write direct SQL queries, then another option is ORMs like:

  1. Entity Framework
  2. Linq to SQL

Here's a related post: https://stackoverflow.com/questions/3505/what-are-your-favorite-net-object-relational-mappers-orm

Depending on how much database interactions you need, you may find the few lines to execute a SQL command query directly easier. If you need to do quite a bit of DB interactions with objects then an ORM may be more productive for you.

Code samples are outside the scope of a question because you need to setup mappings etc... but here's a getting started with tutorial: http://msdn.microsoft.com/en-us/library/bb386876.aspx

Sign up to request clarification or add additional context in comments.

Comments

2

You might want to find an ORM.

Linq to Sql and Entity Framework are a couple you can use out of the box in .Net.

2 Comments

You'll need to create a datasource in your .Net project to use either, then objects based on your SQL tables/views will be generated for you to use in your code. There are many examples for both Linq 2 Sql and EF on the web.
1

You can use linq to sql. Here is a some examples

Comments

0

Sorry I am not sure I understand your question, but I do not think there is a way to get 'any' data from DATABASE without querying. You may have to use a simple select query to return the data from a table or a stored procedure.

Comments

0

Framework 2.0 doesn't support LINQ! U can't use entity framework or LINQ in framework 2.0. Try framewerk 4.0. If not u have to write a query.

Comments

0

You can use Strongly Typed DataSet File and then return data into your label

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.