c# - Column names are not saving in text file while exporting from SQL -
i exporting table sql in text file, data exporting fine column names not coming in text file. here code:
public static void getfiles() { sqldatareader reader; string query = "select * ttable"; string connstr = configurationmanager.connectionstrings["myconnectionstring"].connectionstring; string strdelimiter = "\"|\""; string strfilepath = @"path"; stringbuilder sb = new stringbuilder(); using (sqlconnection conn = new sqlconnection(connstr)) { conn.open(); using (reader = new sqlcommand(query, conn).executereader()) { if (reader.hasrows) { object[] items = new object[reader.fieldcount]; while (reader.read()) { reader.getvalues(items); foreach (var item in items) { sb.append(item.tostring()); sb.append(strdelimiter); } sb.append("\n"); } } } conn.close(); file.writealltext(strfilepath, sb.tostring()); } }
i don't know changes make in method
you don't write column names. can achieved reader.getname()
method this:
using (reader = new sqlcommand(query, conn).executereader()) { (int i=0; i<reader.fieldcount; i++) { sb.append(reader.getname(i)); sb.append(strdelimiter); } sb.append(environment.newline); // code here... if (reader.hasrows) { // etc... } }
Comments
Post a Comment