Sunday, March 14, 2010

Connect c# to sql

C# Class exemple for database work

using System;
using System.Text;
using System.Data.SqlClient;
using System.Data;

class Locations
{
private int _Id;
private String _Location;

public Locations()
{
_Id = 0;
_Location = "";
}

public Locations(int m_Id, String m_Location)
{
_Location = m_Location;
}

public static DataTable GetMultipleLocations()
{
SqlConnection conn = new SqlConnection("");
conn.Open();
SqlCommand command = new SqlCommand();
command.Connection = conn;
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "procedura";
DataTable tabel = new DataTable();
SqlDataAdapter dt = new SqlDataAdapter(command);
dt.Fill(tabel);
return tabel;
}

public void GetLocations(String Id)
{
SqlConnection conn = new SqlConnection("");
conn.Open();
SqlCommand command = new SqlCommand();
command.Connection = conn;
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "procedura";
SqlParameter p = new SqlParameter("Id", SqlDbType.Int);
command.Parameters[0].Value = Id;
SqlDataReader dr = command.ExecuteReader();
while (dr.Read())
{
_Id = dr.GetInt32(0);
_Location = dr.GetString(2);
}
}
public int Id
{
get { return _Id; }
set { _Id = value; }
}
public String Location
{
get { return _Location; }
set { _Location = value; }
}
}

No comments:

Post a Comment