需要您的帮助,我是一名新程序员,想向您学习一些东西 [英] need your help, i am a new programmer, want to learn something from you
本文介绍了需要您的帮助,我是一名新程序员,想向您学习一些东西的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有人可以告诉我如何将word,pdf,excell等文件上传到SQL Server 2005以及如何检索它们.
Can anybody tell me how to upload word, pdf, excell etc file to SQL server 2005 and how to retrieve them.
推荐答案
将它们读取为数组字节:
Read them as an array of bytes:
byte[] bytes = File.ReadAllBytes(@"D:\Temp\MyFile.doc");
然后将它们插入表中:
Then insert them into your table:
using (SqlConnection con = new SqlConnection(strConnect))
{
con.Open();
using (SqlCommand com = new SqlCommand("INSERT INTO myTable (myFileDataColumn) VALUES (@DATA)", con))
{
com.Parameters.AddWithValue("@DATA", bytes);
com.ExecuteNonQuery();
}
}
将它们读回非常相似:
To read them back is very, very similar:
using (SqlConnection con = new SqlConnection(strConnect))
{
con.Open();
using (SqlCommand com = new SqlCommand("SELECT myFileDataColumn FROM myTable", con))
{
using (SqlDataReader reader = com.ExecuteReader())
{
while (reader.Read())
{
byte[] bytes = (byte() reader["myFileDataColumn "];
File.WriteAllBytes(@"D:\Temp\MyFileBackAgain.doc", bytes);
}
}
}
}
看看这是否对您有帮助:
通过C#访问SQL Server的入门指南 [ ^ ]
See if this helps you:
Beginners guide to accessing SQL Server through C#[^]
然后您需要学习的第一件事就是google是您的朋友.
First thing that you need to learn then is google is your friend.
这篇关于需要您的帮助,我是一名新程序员,想向您学习一些东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文