C# Merging the results from SQL queries in datagridview -
good day,
what i'm working on right now, have foreach loop going trough file list of servers. every time goes trough queries different server list provided text file. results displayed in datagridview. right every time querying new server, overwrites data in datagridview. tried merge datatable not working. idea?
try { string select = "select @@servername servername, @@servicename instance,serverproperty('productversion') version, serverproperty ('productlevel') level, serverproperty ('edition') edition"; cnn.open(); // messagebox.show ("connection open ! "); messagebox.show("connection established"); sqldataadapter dataadapter = new sqldataadapter(select, cnn); sqlcommandbuilder commandbuilder = new sqlcommandbuilder(dataadapter); datatable table = new datatable(); table.locale = system.globalization.cultureinfo.invariantculture; dataadapter.fill(table); datagridview.datasource = table; table.merge(table); cnn.close(); //paintrows(); } catch (exception ex) { messagebox.show("can not open connection ! " + ex); }
the problem need append table, you're creating new 1 in every loop iteration.
what i'd is: create method takes target table variable (so same in every call).
outline of this:
**** code somewhere fill table **** datatable table = new datatable(); foreach (line in file) { // create connection new server cnn = ...; // append results existing table appendsqlresults(table, cnn); } // set source after collecting data datagridview.datasource = table; **** method appends data table **** public void appendsqlresults(datatable table, sqlconnection cnn) { cnn.open(); sqldataadapter dataadapter = new sqldataadapter(select, cnn); sqlcommandbuilder commandbuilder = new sqlcommandbuilder(dataadapter); table.locale = system.globalization.cultureinfo.invariantculture; dataadapter.fill(table); cnn.close(); }
Comments
Post a Comment