将sql表作为按钮下载到Excel的最简单方法是什么?只需从tableA中选择*,包含所有数据。
我也想使用这种格式......
SqlConnection sqlCon2 = new SqlConnection("REMOVED");
SqlCommand sqlCmd2 = new SqlCommand();
sqlCmd2.CommandText = "SELECT QtyOnHand FROM Products.Products WHERE PartNumber like '" + textBox1.Text + "'";
sqlCmd2.Connection = sqlCon2;
sqlCon2.Open();
//Download Excel Code
sqlCon2.Close();
谢谢,我很感激:)
答案 0 :(得分:2)
在SSMS中执行您的查询,选择所有结果,右键单击并将结果另存为... ,这样您就可以将结果保存为文件。
答案 1 :(得分:0)
正如评论所说,只输出一个以逗号分隔的文件:
SqlConnection sqlCon2 = new SqlConnection("REMOVED");
SqlCommand sqlCmd2 = new SqlCommand();
sqlCmd2.CommandText = "SELECT QtyOnHand FROM Products.Products WHERE PartNumber like '" + textBox1.Text + "'";
sqlCmd2.Connection = sqlCon2;
sqlCon2.Open();
using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"excelfile.csv"))
{
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
file.WriteLine(reader[0]);
}
reader.Close();
}
sqlCon2.Close();