Click here to Skip to main content
15,885,869 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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
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.

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.
 
Share this answer
 
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[^]
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900