////////////////////////////////////////////////////
input a word document into the sql-server storage
notice :
the type of word document in sql-server database is image
the table structure in sql-serve
blob表
id int 4 0
name char 50
blob image 16
type char 60
input a word document into the sql-server storage
notice :
the type of word document in sql-server database is image
the table structure in sql-serve
blob表
id int 4 0
name char 50
blob image 16
type char 60
protected void Button1_Click(object sender, EventArgs e)
{
//File1 is a component of c# is Fileupload
Stream imgdatastream = File1.PostedFile.InputStream;
int imgdatalen = File1.PostedFile.ContentLength;
string imgtype = File1.PostedFile.ContentType;
string name = this.File1.PostedFile.FileName;
byte[] imgdata = new byte[imgdatalen];
int n = imgdatastream.Read(imgdata, 0, imgdatalen);
string connstr = "Data Source=LIU;Initial Catalog=YJSBDB;User ID=sa";
SqlConnection connection = new SqlConnection(connstr);
SqlCommand command = new SqlCommand("INSERT INTO blob(name,type,blob) VALUES ( @imgtitle, @type,@blob )", connection);
SqlParameter paramTitle = new SqlParameter("@imgtitle", SqlDbType.VarChar, 50);
paramTitle.Value = name;
command.Parameters.Add(paramTitle);
SqlParameter paramData = new SqlParameter("@blob", SqlDbType.Image);
paramData.Value = imgdata;
command.Parameters.Add(paramData);
SqlParameter paramType = new SqlParameter("@type", SqlDbType.VarChar, 50);
paramType.Value = imgtype;
command.Parameters.Add(paramType);
connection.Open();
int numRowsAffected = command.ExecuteNonQuery();
connection.Close();
}
{
//File1 is a component of c# is Fileupload
Stream imgdatastream = File1.PostedFile.InputStream;
int imgdatalen = File1.PostedFile.ContentLength;
string imgtype = File1.PostedFile.ContentType;
string name = this.File1.PostedFile.FileName;
byte[] imgdata = new byte[imgdatalen];
int n = imgdatastream.Read(imgdata, 0, imgdatalen);
string connstr = "Data Source=LIU;Initial Catalog=YJSBDB;User ID=sa";
SqlConnection connection = new SqlConnection(connstr);
SqlCommand command = new SqlCommand("INSERT INTO blob(name,type,blob) VALUES ( @imgtitle, @type,@blob )", connection);
SqlParameter paramTitle = new SqlParameter("@imgtitle", SqlDbType.VarChar, 50);
paramTitle.Value = name;
command.Parameters.Add(paramTitle);
SqlParameter paramData = new SqlParameter("@blob", SqlDbType.Image);
paramData.Value = imgdata;
command.Parameters.Add(paramData);
SqlParameter paramType = new SqlParameter("@type", SqlDbType.VarChar, 50);
paramType.Value = imgtype;
command.Parameters.Add(paramType);
connection.Open();
int numRowsAffected = command.ExecuteNonQuery();
connection.Close();
}
//////////////////////////////////////////////////////////////
output the word document from sql-server database
//////////////////////////////////////////////////////////////
protected void Page_Load(object sender, EventArgs e)
{
string imgid = this.Request.QueryString.Get("ID");
//Request.QueryString["imgid"];
string connstr = "Data Source=LIU;Initial Catalog=YJSBDB;User ID=sa";
string sql = "SELECT name,blob, type FROM blob ";
SqlConnection connection = new SqlConnection(connstr);
SqlCommand command = new SqlCommand(sql, connection);
connection.Open();
SqlDataReader dr = command.ExecuteReader();
if (dr.Read())
{
Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
//Response.ContentType = "application/ms-word";//设置输出文件类型为word文件。
Response.ContentType = dr["type"].ToString();
Response.BinaryWrite((byte[])dr["blob"]);
string FileName = dr["name"].ToString().Trim();
FileName = System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8);
Response.AppendHeader("Content-Disposition", "attachment;filename=" + FileName);
}
connection.Close();
}
output the word document from sql-server database
//////////////////////////////////////////////////////////////
protected void Page_Load(object sender, EventArgs e)
{
string imgid = this.Request.QueryString.Get("ID");
//Request.QueryString["imgid"];
string connstr = "Data Source=LIU;Initial Catalog=YJSBDB;User ID=sa";
string sql = "SELECT name,blob, type FROM blob ";
SqlConnection connection = new SqlConnection(connstr);
SqlCommand command = new SqlCommand(sql, connection);
connection.Open();
SqlDataReader dr = command.ExecuteReader();
if (dr.Read())
{
Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
//Response.ContentType = "application/ms-word";//设置输出文件类型为word文件。
Response.ContentType = dr["type"].ToString();
Response.BinaryWrite((byte[])dr["blob"]);
string FileName = dr["name"].ToString().Trim();
FileName = System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8);
Response.AppendHeader("Content-Disposition", "attachment;filename=" + FileName);
}
connection.Close();
}
http://www.cnblogs.com/liulanglang/archive/2007/06/25/794617.aspx