c# - How can I read remote sql tables into a winforms program? -
i have sql server table located on website (remote). table called table1
, contains bunch of fields. goal here read fields of table1
array called results
.
here attempt:
private static void showfields() { using (sqlconnection connection = new sqlconnection(connectionstring)) { connection.open(); sqlcommand command = new sqlcommand("select * information_schema.columns table_name='table1'", connection); string[] results = command.beginexecutenonquery().toarray(); connection.close(); foreach (var v in results) { console.writeline(v); } } }
you need use sqlcoomand.executereader()
instead of:
private static void showfields() { using (sqlconnection connection = new sqlconnection(connectionstring)) { connection.open(); sqlcommand command = new sqlcommand("select * information_schema.columns table_name='table1'", connection); sqldatareader reader = command.executereader(); connection.close(); int colcount = reader.fieldcount; while (reader.read()) { (int = 0; < colcount; i++) { console.writeline(reader.getstring(i)); } } } }
Comments
Post a Comment