how to run sql query on all column data and export result to csv -in c# -
i made sql queries on access db. in datagridview2 see in first column istalled programs in second how many computers installed program.
col1 col2 xxxx 1 yyyy 2 zzzz 3 oledbcommand command2 = new oledbcommand(); command2.connection = connection; string query = "select item_1, count(item_1) (select item_1 audit_data category_id = 500) group item_1 having (count(*)>0) "; command2.commandtext = query; oledbdataadapter da1 = new oledbdataadapter(command2); da1.fill(dt2); datagridview2.datasource = dt2; datagridview2.autoresizecolumns();
the datagridview3 contains computers name programs installed when selection changed on datagridview2:
string selcell = convert.tostring(datagridview2.currentcell.value); oledbcommand command3 = new oledbcommand(); command3.connection = connection; string query = "select distinct fully_qualified_domain_name audit_data, computer_master item_1= '"+selcell+"' , category_id=500 , audit_data.computer_id = computer_master.computer_id "; command3.commandtext = query; oledbdataadapter da3 = new oledbdataadapter(command3); da3.fill(dt3); datagridview3.datasource = dt3; datagridview3.autoresizecolumns();
and run these queries software installed computer names. don't know how run query on data on col 1 , export csv this.
xxxx; 1; qwer_pc yyyy; 2; asdf_pc; qwer_pc zzzz; 3; asdf_pc; qwer_pc; yxcv_pc
could solve problem? or can somehow combine 2 queries?
the solution easier thought:
select distinct item_1,fully_qualified_domain_name audit_data,computer_master category_id = 500 , audit_data.computer_id = computer_master.computer_id
the csv export following :
string csv = string.empty; //add header row csv file. foreach (datagridviewcolumn column in datagridview4.columns) { csv += column.headertext + ','; } //add new line. csv += "\r\n"; //adding rows foreach (datagridviewrow row in datagridview4.rows) { foreach (datagridviewcell cell in row.cells) { //add data rows. csv += cell.value.tostring().replace(",", ";") + ','; } //add new line. csv += "\r\n"; } //exporting csv. string folderpath = txt_csv_exp_path.text; file.writealltext(folderpath +txt_exp_file_name.text +" .csv", csv); messagebox.show("csv file saved.");
Comments
Post a Comment