public class dbCon
{
string strconnection="Data Source=Rajitha-PC\\SQLexpress;Initial Catalog=POS;User ID=etutor_master_admin;Password=123;Integrated Security=True";
SqlConnection sqlcon = new SqlConnection();
SqlCommand sqlcmd = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();
DataTable dt = new DataTable();
public void connect()
{
sqlcon = new SqlConnection(strconnection);
sqlcon.Open();
}
public void disconnect()
{
if (sqlcon.State == ConnectionState.Open)
{
sqlcon.Close();
sqlcon.Dispose();
}
}
public DataTable ReadData(string query)
{
try
{
connect();
sqlcmd = new SqlCommand(query, sqlcon);
da = new SqlDataAdapter(sqlcmd);
dt = new DataTable();
da.Fill(dt);
}
catch (Exception ex)
{
throw ex;
}
finally
{
disconnect();
}
return dt;
}
public void QryCommand(string query)
{
try
{
connect();
sqlcmd = new SqlCommand(query, sqlcon);
sqlcmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw ex;
}
finally
{
disconnect();
}
}
Thats it..
Now Yo can call these from your coding windows
dbCon db = new dbCon();
DataTable dt=new DataTable();
dt = db.ReadData("select * from emp");
DataGridView1.DataSource=dt;
No comments:
Post a Comment