c# - Using Returned SQL Data in command line, and ultimately winforms -


essentially want request rows sql server , make use of data returns.

select customer, postcode all_customers hometown = london 

i want first print of returned fields using console.writeline , assign values in each of rows variables further manipulation.

i have string below appears compile fine, when comes running program, not seem able interact data returned, if indeed returned @ all. have tried number of solutions no success.

sqlconnection sqlconnection1 = new sqlconnection("connection string");  sqlcommand cmd = new sqlcommand(); sqldatareader reader;  cmd.commandtext = "sql query"; cmd.commandtype = commandtype.text; cmd.connection = sqlconnection1;  sqlconnection1.open();  reader = cmd.executereader();  sqlconnection1.close(); 

you need read records, returned call executereader().

using (var reader = cmd.executereader()) {     // loop through records     while (reader.read())     {         // value in columns 1 , 2, assuming they're string , int         var somevalue = reader.getstring(0);         var someid = reader.getint32(1);          // or column name:         var somevalue = reader["some_column"].tostring();         var someid = convert.toint32(reader["another_column"]);     } } 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -