public override void VerifyRenderingInServerForm(Control control)
{
}
protected void btnExport_Click(object sender, System.EventArgs e)
{
string connectionString = WebConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
using (SqlConnection connection = new SqlConnection(connectionString))
{
try
{
connection.Open();
}
catch (System.Data.SqlClient.SqlException ex)
{
// handle
return;
}
string selectCommandText = "SELECT * FROM Students";
using (SqlDataAdapter adapter = new SqlDataAdapter(selectCommandText, connection))
{
using (DataTable table = new DataTable("Students"))
{
adapter.Fill(table);
StringBuilder commaDelimitedText = new StringBuilder();
//commaDelimitedText.AppendLine("col1,col2,col3"); // optional if you want column names in first row
foreach (DataRow row in table.Rows)
{
string value = string.Format("{0} {1} {2} {3} {4} {5}", row[0], row[1], row[2], row[3], row[4], row[5]); // how you format is up to you (spaces, tabs, delimiter, etc)
commaDelimitedText.AppendLine(value);
}
string str = "CustomName1";
string btr = "CustomName2";
File.WriteAllText(@"C:\\temp\" + str + btr + ".txt", commaDelimitedText.ToString());
}
}
}
}
No comments:
Post a Comment