大家看看那里错了,程序是正常的运行的:存入数据库:
private void button2_Click(object sender, System.EventArgs e)
{
SqlConnection cn = new SqlConnection(cnstr);
if (cn.State == ConnectionState.Closed) cn.Open();
openFileDialog1.ShowDialog();
string filename = openFileDialog1.FileName;
FileInfo fi = new FileInfo(filename);
FileStream fs = fi.OpenRead();
Byte[] bytes = new byte[fs.Length];
string insertstr="insert into wordtable(wordfiles) values(@file)"; SqlCommand cmd = new SqlCommand();
cmd.Connection = cn;
cmd.CommandType=CommandType.Text;
cmd.CommandText = insertstr;
SqlParameter par = new SqlParameter("@file", SqlDbType.Image);
par.Value = bytes;
cmd.Parameters.Add(par);
cmd.ExecuteNonQuery();
MessageBox.Show("this is successful");
cn.Dispose();
}从数据库中读出:private void button3_Click(object sender, System.EventArgs e)
{
SqlConnection cn=new SqlConnection(cnstr);
if(cn.State==ConnectionState.Closed) cn.Open();
string cmdstr="select wordfiles from wordtable where autoid="+this.textBox2.Text.Trim();
SqlCommand cmd=new SqlCommand();
cmd.Connection=cn;
cmd.CommandType=CommandType.Text;
cmd.CommandText=cmdstr;
SqlDataReader dr=cmd.ExecuteReader();
Byte[] file=null;
while(dr.Read())
{
file=(Byte[])dr[0]; }
FileStream fs;
saveFileDialog1.Filter="PDF FILE(*.PDF)|*.PDF| ALL FILE(*.*)|*.DOC";
saveFileDialog1.ShowDialog();
string filepath=saveFileDialog1.FileName;
FileInfo fi=new FileInfo(saveFileDialog1.FileName);
fs=fi.OpenWrite();
fs.Write(file,0,file.Length);
fs.Close();
MessageBox.Show("ok");
}程序是正常运行的,存入和读出的文件大小是一样的,帮助一下!!
private void button2_Click(object sender, System.EventArgs e)
{
SqlConnection cn = new SqlConnection(cnstr);
if (cn.State == ConnectionState.Closed) cn.Open();
openFileDialog1.ShowDialog();
string filename = openFileDialog1.FileName;
FileInfo fi = new FileInfo(filename);
FileStream fs = fi.OpenRead();
Byte[] bytes = new byte[fs.Length];
string insertstr="insert into wordtable(wordfiles) values(@file)"; SqlCommand cmd = new SqlCommand();
cmd.Connection = cn;
cmd.CommandType=CommandType.Text;
cmd.CommandText = insertstr;
SqlParameter par = new SqlParameter("@file", SqlDbType.Image);
par.Value = bytes;
cmd.Parameters.Add(par);
cmd.ExecuteNonQuery();
MessageBox.Show("this is successful");
cn.Dispose();
}从数据库中读出:private void button3_Click(object sender, System.EventArgs e)
{
SqlConnection cn=new SqlConnection(cnstr);
if(cn.State==ConnectionState.Closed) cn.Open();
string cmdstr="select wordfiles from wordtable where autoid="+this.textBox2.Text.Trim();
SqlCommand cmd=new SqlCommand();
cmd.Connection=cn;
cmd.CommandType=CommandType.Text;
cmd.CommandText=cmdstr;
SqlDataReader dr=cmd.ExecuteReader();
Byte[] file=null;
while(dr.Read())
{
file=(Byte[])dr[0]; }
FileStream fs;
saveFileDialog1.Filter="PDF FILE(*.PDF)|*.PDF| ALL FILE(*.*)|*.DOC";
saveFileDialog1.ShowDialog();
string filepath=saveFileDialog1.FileName;
FileInfo fi=new FileInfo(saveFileDialog1.FileName);
fs=fi.OpenWrite();
fs.Write(file,0,file.Length);
fs.Close();
MessageBox.Show("ok");
}程序是正常运行的,存入和读出的文件大小是一样的,帮助一下!!
解决方案 »
- 江湖救急, WCF 服务报错,Why???
- 求助.C#客户端和服务端之间通信乱码
- 打开软件 就要启动定时器
- 如何减少多线程占用的资源
- Dreamweaver XP 2004或以上版本,在ASP.NET下的C#链接SQL Server 2005数据库问题
- 弱弱的问题(3)
- 100分再求:怎样用自己的代码实现QQ下线、上线、隐身等????
- 我这个DATAGRIDVIEW绑定了一个datatable,我更新datatable的时候,gridview响应会假死!
- 我用C#.NET做了个小程序,请问怎么能让他随机器的启动而运行。谢谢!
- C#如何把数组传值给JS函数?
- 一个简单的xpath问题,请各位高手进来!
- 我想用c#三层结构的oa换c#三层结构的全站程序学习。
while(dr.Read())
{
file=(Byte[])dr[0]; }
=================================BinaryWriter.Write
{
byte[] stream=(byte[])dr[0];
System.IO.FileStream fs=new FileStream(filePath,FileMode.Create,FileAccess.Write);
fs.Write(stream,0,stream.Length);
fs.Close();
}
//FileStream fs;
System.IO.BinaryWriter fs=null;
saveFileDialog1.Filter="PDF FILE(*.PDF)|*.PDF| ALL FILE(*.*)|*.DOC";
saveFileDialog1.ShowDialog();
string filepath=saveFileDialog1.FileName;
//FileInfo fi=new FileInfo(saveFileDialog1.FileName);
FileStream fi=new FileStream(saveFileDialog1.FileName,FileMode.Create);
//fs=fi.OpenWrite();
fs.Write(file,0,file.Length);
//fs.Write(file
fs.Close();
MessageBox.Show("ok");