c# DbHelper
class DBHelper { public static string connstr = "server=;database;uid=;pwd=;"; public int ExecuteNoQuery(string sql,params SqlParameter[] param) { using (SqlConnection conn = new SqlConnection(connstr)) { conn.Open(); using(SqlCommand cmd=conn.CreateCommand()) { cmd.CommandText = sql; cmd.Parameters.AddRange(param); return cmd.ExecuteNonQuery(); } } } public Object ExecuteScalar(string sql, params SqlParameter[] param) { using (SqlConnection conn = new SqlConnection(connstr)) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = sql; cmd.Parameters.AddRange(param); return cmd.ExecuteScalar(); } } } public DataTable ExecuteTable(string sql,params SqlParameter[] param) { using (SqlConnection conn = new SqlConnection(connstr)) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = sql; cmd.Parameters.AddRange(param); SqlDataAdapter adapter = new SqlDataAdapter(cmd); DataTable dt = new DataTable(); adapter.Fill(dt); return dt; } } } public void FillListView(ListView lv,DataTable dt,params string[] headers) { lv.Clear(); lv.BeginUpdate(); for (int i = 0; i < headers.Length;i++ ) { lv.Columns.Add(headers[i],lv.Width/headers.Length-2); } for (int i = 0; i < dt.Rows.Count;i++ ) { ListViewItem item = new ListViewItem(); item.Text = dt.Rows[i][0] + ""; for(int j=1;j<dt.Columns.Count;j++) { item.SubItems.Add(dt.Rows[i][j] + ""); } lv.Items.Add(item); } lv.EndUpdate(); } }