c# - SQLCommand not returning anything in Writeline -
i trying set sqlconnection our database return datatypes of columns / rows (this needs done can enter data api). data being placed in datatable , through hashtable type.
the problem when program run, no errors occur not return in console writelines have been specified. i'm not overly experienced .net developer i'm not sure i'm missing, guess in order of how sql command / connection opened?
static void main(string[] args) { hashtable sqldatatypeholder = new hashtable(); //sql connection string _mysqlurl = "connection correct , works in other test apps"; string _mysqlquery = "query here, works fine in sql management studio"; sqlconnection conn = new sqlconnection(_mysqlurl); sqlcommand comm = new sqlcommand(_mysqlquery, conn); datatable _temptable = new datatable(); using (conn) { sqlcommand command = new sqlcommand(_mysqlquery,conn); conn.open(); sqldatareader reader = command.executereader(); if (reader.hasrows) { while (reader.read()) { (int v = 0; v < _temptable.columns.count; v++) { datacolumn dc = _temptable.columns[v]; sqldatatypeholder.add(dc.columnname.tostring(), convert.tostring(reader.getsqlvalue(v).gettype())); } foreach (dictionaryentry hr in sqldatatypeholder) { console.writeline(hr.key + " " + hr.value); } } } else { console.writeline("connection open - no rows found."); } reader.close(); } //console.writeline("created sheet " + smartsheetid); console.readline();
}
your _temptable
table
empty not filling that's why unable anything. try code hope help.
datatable _temptable = new datatable(); sqldataadapter = new sqldataadapter("your query",conn); a.fill(_temptable);
you can use code work
static void main(string[] args) { hashtable sqldatatypeholder = new hashtable(); //sql connection string _mysqlurl = "connection correct , works in other test apps"; string _mysqlquery = "query here, works fine in sql management studio"; sqlconnection conn = new sqlconnection(_mysqlurl); sqlcommand comm = new sqlcommand(_mysqlquery, conn); datatable _temptable = new datatable(); using (conn) { sqlcommand command = new sqlcommand(_mysqlquery, conn); conn.open(); sqldatareader reader = command.executereader(); _temptable.load(reader); if (_temptable != null && _temptable.rows.count > 0) { (int v = 0; v < _temptable.columns.count; v++) { datacolumn dc = _temptable.columns[v]; sqldatatypeholder.add(dc.columnname.tostring(), convert.tostring(reader.getsqlvalue(v).gettype())); } foreach (dictionaryentry hr in sqldatatypeholder) { console.writeline(hr.key + " " + hr.value); } } else { console.writeline("connection open - no rows found."); } reader.close(); } }
Comments
Post a Comment