1、将文件转成流存入数据库Stream fileDataStream = MyFile.PostedFile.InputStream;/得到文件大小int fileLength = MyFile.PostedFile.ContentLength;/创建数组byte fileData = new bytefileLength;/把文件流填充到数组fileDataStream.Read(fileData,0,fileLength);/得到文件名字string fileTitle = MyFileName.Value;/得到文件类型string fileType = MyFile.PostedFile.
2、ContentType;/构建数据库连接,SQL 语句,创建参数SqlConnection connection = new SqlConnection(“Server=127.0.0.1;uid=sa;pwd=gotomis;Database=shop“);SqlCommand command = new SqlCommand (“INSERT INTO TestFiles (MyFileName,MyFile,FileType)“ +“VALUES (MyFileName,MyFile,FileType)“, connection);SqlParameter paramTitle = ne
3、w SqlParameter (“MyFileName“, SqlDbType.VarChar,35);paramTitle.Value = fileTitle;command.Parameters.Add(paramTitle);SqlParameter paramData = new SqlParameter (“MyFile“, SqlDbType.Image);paramData.Value = fileData;command.Parameters.Add(paramData);SqlParameter paramType = new SqlParameter (“FileType“, SqlDbType.VarChar,25);paramType.Value = fileType;command.Parameters.Add(paramType);/打开连接,执行查询connection.Open();command.ExecuteNonQuery();connection.Close();Message.Text=“你的文件已经成功上载“;MyFileName.Value = “;