我想写一个sql查询到一个文件,但我只能在文本文件中写一个查询列。如何添加更多列?
这是我的c#windows表单代码:
SqlConnection con = new SqlConnection(@"Data Source=" + globalvariables.hosttxt + "," + globalvariables.porttxt + "\\SQLEXPRESS;Database=ha;Persist Security Info=false; UID='" + globalvariables.user + "' ; PWD='" + globalvariables.psw + "'");
SqlCommand command = con.CreateCommand();
command.CommandText = "Select * from bestillinger";
con.Open();
SqlDataReader queryReader = command.ExecuteReader();
while (queryReader.Read())
{
StreamWriter file = new StreamWriter(@"C:\Users\Michael\Desktop\query.txt");
file.WriteLine(queryReader["ordrenr"]);
file.Close();
}
queryReader.Close();
con.Close();
它不允许我写:
file.WriteLine(queryReader["ordrenr"] + queryReader["user"]);
答案 0 :(得分:0)
我找到了一种方法:
file.WriteLine("{0},{1}", queryReader["ordrenr"], queryReader["user"]);
答案 1 :(得分:0)
static void Main(string[] args)
{
string connString = @"here connection string";
SqlConnection con = new SqlConnection(connString);
SqlCommand command = con.CreateCommand();
command.CommandText = "Select * from Object";
con.Open();
SqlDataReader queryReader = command.ExecuteReader();
StreamWriter file = new StreamWriter(@"C:\Projects\EverydayProject\test.txt");
bool addColumns = false;
string columnName1="Title";
string columnName2 = "City";
while (queryReader.Read())
{
if(addColumns)
{
file.WriteLine(columnName1 + " " + columnName2);
addColumns = true;
}
else
{
file.WriteLine(queryReader["Title"].ToString() + " " + queryReader["City"].ToString());
}
}
queryReader.Close();
con.Close();
file.Close();
}
这是有效的你应该首先使对象成为String(),你也需要在最后关闭文件。不是第一次迭代!
答案 2 :(得分:0)
我现在已经认识了这个六岁的孩子,但是当我在自己的搜索中遇到这个问题时,我觉得提供一个更简洁的答案对其他人也有好处。另外,我还不能发表评论,所以我认为最好把它作为答案。
如Magus在评论中所指出的,OP的回答在重新创建每一行的流时带来了一个相当大的性能问题。
与此同时,mybirthname的答案实际上永远不会以添加标题行结尾,并且如果在创建时将包含的布尔值更改为true,则最终将使文件只包含标题。
在这种情况下,我以逗号分隔值格式写出数据。如果您以后要在电子表格编辑器中打开文件扩展名,则文件扩展名可以是.csv;如果不希望任何最终用户查看,则文件扩展名可以是.txt。
//Consider putting your connection string in a config file and referencing it here.
SqlConnection sqlConn = new SqlConnection(Properties.Settings.Default.ConnString);
//If possible, avoid using "Select *" and instead, select only the columns you care about to increase efficiency.
SqlCommand sqlCmd = new SqlCommand("Select ordrenr, user From bestillinger", sqlConn);
sqlConn.Open();
SqlDataReader sdr = sqlCmd.ExecuteReader();
if (sdr.HasRows)
{
//There's really no reason to create the StreamWriter unless you actually find some data.
StreamWriter swExportWriter = new StreamWriter(@"C:\DataStore\Datafile.csv");
//Now that you know you have data, go ahead and write the first line to the file as the header row.
swExportWriter.WriteLine("ordrenr, user");
//Now use SqlDataReader.Read() to loop through the records and write each one to the file.
while (sdr.Read())
{
swExportWriter.WriteLine("{0},{1}", sdr["ordrenr"], sdr["user"]);
}
//Don't forget to close the StreamWriter!
swExportWriter.Close();
}
sdr.Close();
sqlConn.Close();
根据Magus的建议,如果您想改用Using语句(这可能是个好主意),则也可以像这样构造它:
using (SqlConnection sqlConn = new SqlConnection(Properties.Settings.Default.ConnString))
{
SqlCommand sqlCmd = new SqlCommand("Select ordrenr, user From bestillinger", sqlConn)
sqlConn.Open();
using (SqlDataReader sdr = sqlCmd.ExecuteReader())
{
if (sdr.HasRows)
{
using (StreamWriter swExportWriter = new StreamWriter(@"C:\DataStore\Datafile.csv"))
{
swExportWriter.WriteLine("ordrenr, user");
while (sdr.Read())
{
swExportWriter.WriteLine("{0},{1}", sdr["ordrenr"], sdr["user"]);
}
}
}
}
}