下面两段代码分别是实现向sql数据库中存和取word文件(包含表格):
//读取word并存入sql数据库
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "doc文件(*.doc)|*.doc|所有文件(*.*)|*.*";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
if (openFileDialog1.FileName != "")
{
string mp = openFileDialog1.FileName;
FileStream fs = File.OpenRead(mp); System.IO.BinaryReader br = null;
br = new BinaryReader((Stream)fs);
byte[] b = new byte[fs.Length]; int intLength = (int)fs.Length;
b = br.ReadBytes(intLength);
fs.Close();
string nn = "server=localhost; database=hyz; User ID=sa;trusted_connection=true";
string sql = "insert into test values ('1','" + b + "')";
SqlConnection connect = new SqlConnection(nn);
SqlCommand cmd1 = new SqlCommand(sql);
cmd1.Connection = connect; //数据库连接
cmd1.Parameters.Add("t1", SqlDbType.Image).Value = b;
cmd1.Connection.Open();
cmd1.ExecuteNonQuery(); //入库
cmd1.Connection.Close();
cmd1.Dispose();
connect.Dispose();
connect.Close();
//从数据库中取出,并保存为word文档
string str = "server=localhost;database=hyz;uid=sa;pwd=";
SqlConnection connection = new SqlConnection(str);
SqlCommand cmd = new SqlCommand(sqlstr);
cmd.Connection = connection;
connection.Open();
int intLenth = 0;
MemoryStream stream = new MemoryStream(b, true);
stream.Write(b, 0, intLenth);
FileStream fsw = new FileStream(@"e:\test.doc",FileMode.OpenOrCreate,FileAccess.Write);
fsw.Write(b,0,b.Length)
//读取word并存入sql数据库
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "doc文件(*.doc)|*.doc|所有文件(*.*)|*.*";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
if (openFileDialog1.FileName != "")
{
string mp = openFileDialog1.FileName;
FileStream fs = File.OpenRead(mp); System.IO.BinaryReader br = null;
br = new BinaryReader((Stream)fs);
byte[] b = new byte[fs.Length]; int intLength = (int)fs.Length;
b = br.ReadBytes(intLength);
fs.Close();
string nn = "server=localhost; database=hyz; User ID=sa;trusted_connection=true";
string sql = "insert into test values ('1','" + b + "')";
SqlConnection connect = new SqlConnection(nn);
SqlCommand cmd1 = new SqlCommand(sql);
cmd1.Connection = connect; //数据库连接
cmd1.Parameters.Add("t1", SqlDbType.Image).Value = b;
cmd1.Connection.Open();
cmd1.ExecuteNonQuery(); //入库
cmd1.Connection.Close();
cmd1.Dispose();
connect.Dispose();
connect.Close();
//从数据库中取出,并保存为word文档
string str = "server=localhost;database=hyz;uid=sa;pwd=";
SqlConnection connection = new SqlConnection(str);
SqlCommand cmd = new SqlCommand(sqlstr);
cmd.Connection = connection;
connection.Open();
int intLenth = 0;
MemoryStream stream = new MemoryStream(b, true);
stream.Write(b, 0, intLenth);
FileStream fsw = new FileStream(@"e:\test.doc",FileMode.OpenOrCreate,FileAccess.Write);
fsw.Write(b,0,b.Length)
解决方案 »
- vs2010,文件修改后 上面不出*号了.怎么办啊?
- 请教的Process.Start()问题
- 请问怎么样动态生成HashSet<Person>泛型?这样为何不行?
- CS 登陆 BS 呢
- 如何在一个DataGridView中的一列添加DateTimePicker控件
- c#可以做B/S的东西吗?推荐两本相关的书籍!
- gdi+问题
- 高手请进,一般的也可以进,最菜的也可以进,因为我是超菜。。。问题如下:
- 我写的这个简单的递归函数还能优化吗?
- 请问如何才能将模型显示出来(C#+CSGL读取obj模型)
- 解析HTML类,高手救命!写到这不知道要怎么写了.....
- 关于c#数组的问题。I think it's easy question
string str = "server=localhost;database=hyz;uid=sa;pwd=";
SqlConnection connection = new SqlConnection(str);
SqlCommand cmd = new SqlCommand(sqlstr);
cmd.Connection = connection;
connection.Open();
int intLenth = 0;
MemoryStream stream = new MemoryStream(b, true);
stream.Write(b, 0, intLenth);
FileStream fsw = new FileStream(@"e:\test.doc",FileMode.OpenOrCreate,FileAccess.Write);
fsw.Write(b,0,b.Length)