how to retrieve data from sql server to DataTable using c# -
string j = "data source=fujitsu-pc\\sqlexpress;initial catalog=attendance_system;integrated security=true"; datatable dt = new datatable(); using (sqlconnection cn = new sqlconnection(j)) { sqlcommand cmd = new sqlcommand("select dbo.faculty_status.username dbo.faculty_status", cn); cn.open(); sqldatareader dr = cmd.executereader(); dr.read(); dt.load(dr); cn.close(); } int x = dt.rows.count; foreach (datarow row in dt.rows) { foreach (var item in row.itemarray) { string xx = item.tostring(); } }
i used code wors not show me desire first row value givs me out put like----
13-24516-2
i think problem way reading. try this:
string j = "data source=fujitsu-pc\\sqlexpress;initial catalog=attendance_system;integrated security=true"; using (sqlconnection cn = new sqlconnection(j)) { sqlcommand cmd = new sqlcommand("select dbo.faculty_status.username dbo.faculty_status", cn); cn.open(); using (var reader = cmd.executereader()) { while(reader.read()) { console.writeline(reader["username"]); } } }
let me know if works!
Comments
Post a Comment