将word文档保存到数据库中
在C#winform中,如何把word文档转换成byte[],然后把byte[]转换成word文档,这怎么写啊,那个高手可以给出这两个方法啊先谢谢了,高手你好啊,非常感谢...
在C# winform中,如何把word文档转换成byte[],然后把byte[]转换成word文档,这怎么写啊,那个高手可以给出这两个方法啊先谢谢了,
高手你好啊,非常感谢你的方法,我还想问一个问题,就是save方法这里,你这里转换成word的时候是直接保存在本地了,可不可以不保存在本地,保存在内存中然后打开啊? 展开
高手你好啊,非常感谢你的方法,我还想问一个问题,就是save方法这里,你这里转换成word的时候是直接保存在本地了,可不可以不保存在本地,保存在内存中然后打开啊? 展开
1个回答
展开全部
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.IO;
namespace ConsoleApplication2
{
class Program
{
//把文件写入数据库
public void add(string pathName)
{
FileStream fs = new FileStream(pathName, FileMode.Open, FileAccess.Read);
byte[] buffByte = new byte[fs.Length];
fs.Read(buffByte, 0, (int)fs.Length);
fs.Close();
fs = null;
SqlConnection conn = new SqlConnection(@"data source=test;uid=sa;pwd=test;database=test");
string sqlstr = @"Insert into table1(doc) values(@img)";
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = sqlstr;
cmd.Connection = conn;
cmd.Parameters.Add("@img", System.Data.SqlDbType.Image);
cmd.Parameters[0].Value = buffByte;
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
buffByte = null;
}
//从数据库读出文件
public void save(string fileName)
{
SqlConnection conn = new SqlConnection(@"data source=test;uid=sa;pwd=test;database=test");
conn.Open();
SqlCommand cmd = new SqlCommand("select top 1 doc from table1", conn);
SqlDataReader reader = cmd.ExecuteReader();
byte[] buffByte = null;
if (reader.Read())
{
buffByte = (byte[])reader[0];
}
reader.Close();
conn.Close();
FileStream fs;
FileInfo fi = new FileInfo(fileName);
fs = fi.OpenWrite();
fs.Write(buffByte, 0, buffByte.Length);
fs.Close();
}
static void Main(string[] args)
{
Program p = new Program();
p.add(@"C:\test.doc");
p.save(@"C:\test1.doc");
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.IO;
namespace ConsoleApplication2
{
class Program
{
//把文件写入数据库
public void add(string pathName)
{
FileStream fs = new FileStream(pathName, FileMode.Open, FileAccess.Read);
byte[] buffByte = new byte[fs.Length];
fs.Read(buffByte, 0, (int)fs.Length);
fs.Close();
fs = null;
SqlConnection conn = new SqlConnection(@"data source=test;uid=sa;pwd=test;database=test");
string sqlstr = @"Insert into table1(doc) values(@img)";
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = sqlstr;
cmd.Connection = conn;
cmd.Parameters.Add("@img", System.Data.SqlDbType.Image);
cmd.Parameters[0].Value = buffByte;
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
buffByte = null;
}
//从数据库读出文件
public void save(string fileName)
{
SqlConnection conn = new SqlConnection(@"data source=test;uid=sa;pwd=test;database=test");
conn.Open();
SqlCommand cmd = new SqlCommand("select top 1 doc from table1", conn);
SqlDataReader reader = cmd.ExecuteReader();
byte[] buffByte = null;
if (reader.Read())
{
buffByte = (byte[])reader[0];
}
reader.Close();
conn.Close();
FileStream fs;
FileInfo fi = new FileInfo(fileName);
fs = fi.OpenWrite();
fs.Write(buffByte, 0, buffByte.Length);
fs.Close();
}
static void Main(string[] args)
{
Program p = new Program();
p.add(@"C:\test.doc");
p.save(@"C:\test1.doc");
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |