日期:2014-05-18  浏览次数:20759 次

c#读数据库,将数据写入到txt。
c#读数据库,将数据写入到txt。
但是用文本软件(比如EmEditor)打开文件,会报错:Null Characters。 Invalid Characters。
请问什么原因?为什么会有这样的情况。
数据库是oracle,txt字符编码是gb2312。

------解决方案--------------------
/*测试数据表:test3 Create Table test3(ID bigint not null,Files Image null)
*/
private void button1_Click(object sender, EventArgs e)
{
FileStream fs = File.OpenRead(this.textBox1.Text.Trim());
byte[] bytes = new byte[fs.Length];
fs.Read(bytes, 0, bytes.Length);
SqlConnection con = new SqlConnection(ConStr);
SqlCommand cmd = new SqlCommand("", con);
cmd.CommandText = "Insert into tuzhi values('4','B8814','中冷器',@files,'2009-11-10')";
SqlParameter paraFile = new SqlParameter("@files", SqlDbType.Image);
paraFile.Value = bytes;
cmd.Parameters.Add(paraFile);
this.Cursor = Cursors.WaitCursor;
con.Open();
int i = cmd.ExecuteNonQuery();
MessageBox.Show("成功");
con.Close();
this.Cursor = Cursors.Default;
}

private void button2_Click(object sender, EventArgs e)
{
byte[] mypdffile = null;
SqlConnection con = new SqlConnection(ConStr);
SqlCommand cmd = new SqlCommand("", con);
cmd.CommandText = "Select tu From tuzhi Where ID='1'";
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
mypdffile = (byte[])dr.GetValue(0);
}
con.Close();
this.Cursor = Cursors.WaitCursor;
try
{
FileStream fs = new FileStream("D:\\aa1.pdf", FileMode.Create);
fs.Write(mypdffile, 0, mypdffile.Length);
fs.Flush();
fs.Close();
this.webBrowser1.Navigate("D:\\aa1.pdf");
}
catch { }
this.Cursor = Cursors.Default;
}

private void Form1_Load(object sender, EventArgs e)
{
//连接字符串自己改下,这是我的测试数据库的链接字符串
ConStr = "server=192.168.0.2;database=drawing ;uid=sa;Pwd=7612098;";
}

------解决方案--------------------
无效字符,看看数据值
用SQLLDR
------解决方案--------------------
那你用UE打开试试
------解决方案--------------------
不如直接用SQL做,导出为文本文件
------解决方案--------------------

------解决方案--------------------
我做网优的工具,大部分功能都是把数据从数据库取出来进处理后保存为txt,xml,csv等文件,还真没有碰到你这种问题;
不过我用的数据库主要是sql2000,mysql等,
------解决方案--------------------
直接用SQL连接不就的了
------解决方案--------------------
顶一下顶一下
------解决方案--------------------
你有没有做过把txt中的数据导入数据库?
你现在做的就是反过来操作,从数据库一行一行的读,然后一行一行的写入txt中,个人觉得不难,你再看看
------解决方案--------------------
是不是写完后没有关闭流呢。
------解决方案--------------------
DataGridView打印精灵组件 ,欢迎大家使用!一种快速的打印报表解决方案 ~_~ 

下载地址:点击下载体验版(带说明文档)  

我的博客(http://www.cnblogs.com/jyworks) 

  DataGridView打印精灵组件提供您一个高效快速的报表生成与打印解决方案,提供详细快速的设置,让您直接从DataGridView生成报表、完成您的办公。