Click here to Skip to main content
12,300,379 members (52,716 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: ASP.NET SQL ADO.NET
My Emp table is stored in sql server which consist of columns ID,Name,Age & adress.
I want to Display the name of employee in textboxes whose age is 25.
how shoul I connect the database for such a task.
Posted 5-Jan-13 2:19am
Comments
Reza Alipour Fard 5-Jan-13 15:30pm
   
I think you is popular. Therefore I present a class for management connecting to sql and get data.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

This looks like a very basic question. If you are not familiar with ADO.net go through these CP articles.
Using ADO.NET for beginners[^]
Database Manipulation with ADO.NET for beginners[^]
A Beginner's Tutorial for Understanding ADO.NET[^]
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Hi

You can Use from bellow method for manage Database:

public class DBManagement
{
    public SqlConnection OpenConnection()
    {
        string connectionString = "Data Source = [ServerName]; Initial Catalog = [DatabaseName]; User ID = [SqlUserName]; Password = [SqlUserPassword]";
 
        SqlConnection connection = null;
 
        try
        {
            connection = new SqlConnection(connectionString);
            connection.Open();
        }
        catch
        {
            Console.WriteLine("DataBase is not reachable....");
        }
 
        return connection;
 
    }
 
    public DataTable Execute(string queryString)
    {
        SqlConnection connection = OpenConnection();
 
        SqlCommand query = new SqlCommand(queryString, connection);
 
        DataTable resultTable = new DataTable();
 
        SqlDataAdapter adopter = new SqlDataAdapter(query);
 
        adopter.Fill(resultTable);
 
        connection.Close();
 
        return resultTable;
    }
}

Now you can create instance from DBManagement and call execute method by send bellow string az query:

string query = "Select Name From YourTableName Where Age = 25";
DBManagement manage = new DBManagement();
 
DataTable result = manage.Execute(query);

I hope it's helpful.
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160525.2 | Last Updated 5 Jan 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100