保存操作
Stream imgDataStream = this.fileLoad.PostedFile.InputStream;
int imgDataLenth = this.fileLoad.PostedFile.ContentLength;
byte[] imgDataList = new byte[imgDataLenth];
imgDataStream.Read(imgDataList, 0, imgDataLenth);
//数据库连接语句改你的
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRING"].ConnectionString);
string strSQL = "Insert into [unformatdata] (Unfordata) values(@EmployeeImage)";
SqlCommand cmd=new SqlCommand(strSQL,conn);
cmd.Parameters.Add("@EmployeeImage",SqlDbType.Binary);
cmd.Parameters["@EmployeeImage"].Value = imgDataList;
try
{
conn.Open();
int retval = cmd.ExecuteNonQuery();
MessageBox.Show("添加成功!");
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
conn.Close();
读取操作
DataSet myDataSet = new DataSet();
string updateSql = "Select * from unformatdata where ID = 4 ";
//创建sqlCommand对象
string conString = ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRING"].ConnectionString;
SqlConnection myConnection = new SqlConnection(conString);
SqlCommand myCommand = new SqlCommand(updateSql, myConnection);
myCommand.Connection.Open();
try
{
SqlDataAdapter MyAdapter = new SqlDataAdapter();
SqlCommandBuilder Var_SqlCB;
MyAdapter.SelectCommand = myCommand;
Var_SqlCB = new SqlCommandBuilder(MyAdapter);
MyAdapter.Fill(myDataSet, "myTable");
}
catch (Exception ex)
{
System.Console.WriteLine(ex.Message.ToString());
}
DownloadStream(this, (byte[])myDataSet.Tables["myTable"].Rows[0]["Unfordata"],"new.doc");
下载的功能
try
{
string MyContentType = "";
switch (_FileName.Substring(_FileName.LastIndexOf("."), 4))
{
case ".xls":
MyContentType = "application/vnd.ms-excel";
break;
case ".doc":
MyContentType = "application/msword";
break;
default:
MyContentType = "application/zip";
break;
}
refPage.Response.Write("MyContentType");
refPage.Response.Clear();
refPage.Response.AddHeader("Content-Type", MyContentType);
string FileName = System.Web.HttpUtility.UrlEncode(System.Text.Encoding.UTF8.GetBytes(_FileName));
refPage.Response.AddHeader("Content-Disposition", "inline;filename=" + System.Convert.ToChar(34) + FileName + System.Convert.ToChar(34));
refPage.Response.AddHeader("Content-Length", FileData.Length.ToString());
refPage.Response.BinaryWrite(FileData);
refPage.Response.End();
return true;
}
catch (Exception ex)
{
return false;
}
针对数据库字段Unfordata,如果是SQL2000最好用image类型,如果是SQL2005则可以用varbinary(max)