Click here to Skip to main content
15,918,976 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
i design a program in C# , i need to know how can i use data from data base .
such as : i have variable string s,how can i give this variable data from data base (Access or SQL) like name of employee .
Posted
Updated 18-Jun-13 23:26pm
v2

Try:
C#
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand cmd = new SqlCommand("SELECT employeeName FROM myTable WHERE Id=@ID", con))
        {
        cmd.Parameters.AddWithValue("@ID", employeeID);
        using (SqlDataReader reader = cmd.ExecuteReader())
            {
            while (reader.Read())
                {
                string name = (string) reader["employeeName"];
                Console.WriteLine("ID: {0}\n    {1}", employeeId, name);
                }
            }
        }
    }
 
Share this answer
 
Comments
loai_maane 19-Jun-13 5:38am    
thnx :))
Hi You should better first look over the web or search it in codeproject, but hope this link help you.
Beginners guide to accessing SQL Server through C#[^]
 
Share this answer
 
Comments
loai_maane 19-Jun-13 5:38am    
thnx:))
 
Share this answer
 
v2
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace ConnectToDB
{
public class DBConnection
{
static void Main(string[] args)
{
System.Data.SqlClient.SqlConnection connection;
System.Data.SqlClient.SqlCommand command;
System.Data.SqlClient.SqlDataReader reader;
System.Data.SqlClient.SqlParameter param;
try
{
/* Dynamic Connection
Console.WriteLine("Enter DS Name:");
string dsname = Console.ReadLine();
Console.WriteLine("Enter DB Name:");
string dbname = Console.ReadLine();
Console.WriteLine("Enter Integrated Security:");
string iss = Console.ReadLine();
connection = new SqlConnection("Data Source="+dsname+";Initial Catalog="+dbname+";Integrated Security="+iss+""); */
connection = new SqlConnection("Data Source=GAGANDEEP;Initial Catalog=AdventureWorks;Integrated Security=True");
connection.Open();
param = new SqlParameter("@Param1", SqlDbType.Int,5);
Console.WriteLine("Connected To DB");
Console.WriteLine("Enter Location ID:");
string loc = Console.ReadLine();s
// command = new SqlCommand("select * from [AdventureWorks].[Production].[Location] where LocationID<@Param1", connection);
command = new SqlCommand("select * from [AdventureWorks].[Production].[Location] where LocationID<@Param1", connection);
command.Parameters.Add(param).Value=loc;
reader = command.ExecuteReader();
while (reader.Read())
{
Console.WriteLine("Location ID: " + reader["LocationID"].ToString()+"\t"+"Name: " + reader["Name"].ToString());
// Console.WriteLine("Name: " + reader["Name"].ToString());
//Console.WriteLine("Cost Rate: " + reader["Cost Rate"].ToString());
//Console.WriteLine("Availability: " + reader["Availability"].ToString());
//Console.WriteLine("Modified Date: " + reader["Modified Date"].ToString());
}
reader.Close();
}
catch (Exception ex)
{
Console.WriteLine("Failed to connect to data source"+ex.Message);
}
finally
{
Console.WriteLine("Connection Closed Automatically");
Console.ReadKey();
}
}
}
}
 
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