高手请看,C#就SQL中某表的某几列写入到带制表符的TXT文本文档,哪里有错啊?
string str_File = "";
string SaveFileName="";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
SaveFileName = saveFileDialog1.FileName;
}
SaveFileName = SaveFileName + ".txt";
string strConn = string.Format("server={0};uid={1};pwd={2};database={3};", txtServer.Text, txtUserName.Text, txtUserPass.Text, cbmDataBase.Text);
using (SqlConnection conn = new SqlConnection(strConn))
{
try
{
conn.Open();
//加载数据库表
SqlCommand cmd = new SqlCommand("select sno,sname,ssex,sage,sdept from "+cmbTable .Text , conn);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
str_File = str_File + dr["sno"].ToString();
str_File = str_File + dr["sname"].ToString();
str_File = str_File + dr["ssex"].ToString();
str_File = str_File + dr["sage"].ToString();
str_File = str_File + dr["sdept"].ToString();
str_File = str_File + "\r\n";
}
dr.Close();
if (File.Exists(SaveFileName))
{
MessageBox.Show("该文件已经存在!已经被删除,请重新再次导出即可");
File.Delete(SaveFileName);
return;
}
else
{
StreamWriter sw = File.CreateText(SaveFileName);
//创建文本文件
sw.Write(str_File);
MessageBox.Show("写入文件成功");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
我写了这段代码,txt文档是导出了,可是里面没有值,为什么?请高手们指点