C#导出数据出错,提示“空路径名是非法的”,大家帮忙看看吧!
SaveFileDialogsfdExpor=newSaveFileDialog();using(SqlConnectionconn=newSqlConnection(@...
SaveFileDialog sfdExpor = new SaveFileDialog();
using (SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDBFilename=|DataDirectory|\Database2.mdf;Integrated Security=True"))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "select * from T_Users";
using (SqlDataReader reader = cmd.ExecuteReader())
{
using (FileStream fileStream = File.OpenWrite(sfdExpor.FileName))//提示就在这边出错的
{
using (StreamWriter streamWrite = new StreamWriter (fileStream ))
{
while (reader.Read())
{
string name = reader.GetString(reader .GetOrdinal ("Name"));
int age = reader.GetInt32(reader .GetOrdinal ("Age"));
string line = name + '|' + age;
streamWrite.WriteLine(line );
}
MessageBox.Show("导出成功");
}
}
}
}
} 展开
using (SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDBFilename=|DataDirectory|\Database2.mdf;Integrated Security=True"))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "select * from T_Users";
using (SqlDataReader reader = cmd.ExecuteReader())
{
using (FileStream fileStream = File.OpenWrite(sfdExpor.FileName))//提示就在这边出错的
{
using (StreamWriter streamWrite = new StreamWriter (fileStream ))
{
while (reader.Read())
{
string name = reader.GetString(reader .GetOrdinal ("Name"));
int age = reader.GetInt32(reader .GetOrdinal ("Age"));
string line = name + '|' + age;
streamWrite.WriteLine(line );
}
MessageBox.Show("导出成功");
}
}
}
}
} 展开
展开全部
请问你在哪里设置了sfdExpor.FileName吗?调试一下是不是为空。
SaveFileDialog sfdExpor = new SaveFileDialog();
怎么不调用sfdExpor.showDialog()?
SaveFileDialog sfdExpor = new SaveFileDialog();
怎么不调用sfdExpor.showDialog()?
追问
我用FileStream fileStream = File.OpenWrite(sfdExpor.ShowDialog())试了一下,但却发现还是行不通。别见笑,我刚刚接触这个,感觉自己学的很乱很烂。。。
追答
大哥,File.OpenWrite(sfdExpor.ShowDialog())这里你想做什么?
sfdExpor.ShowDialog())返回的是DialogResult的枚举,你要打开的是文件的路径啊!
在using (FileStream fileStream = File.OpenWrite(sfdExpor.FileName))前面加上
if(sfdExpor.ShowDialog()==DialogResult.OK)
{
if(sfdExpor.FileName=="")
return;
}
听我一句,做这一行要学会调试啊,基本的生存技能。不要在上面有任何逃避心理,不然你会很惨!
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询