CREATE TABLE Images ( Roll_no varchar(12) primary key, Name_File varchar(100), Extension varchar(100) , img varbinary(max) , Img_date datetime )
文件序号 <asp:TextBox ID="txtrollno" runat="server"> </asp:TextBox> <br /> 选择文件 <asp:FileUpload ID="FileUpload1" runat="server" /> <br /> <asp:Button ID="Button1" runat="server" Text="上传" OnClick="Button1_Click" />
using System; using System.Data; using System.Data.SqlClient; using System.IO; using System.Web;
protected void Button1_Click(object sender, EventArgs e)
{
if (!FileUpload1.HasFile)
{
Response.Write("未选择文件");
return;
}
else
{
//创建访问上传文件的对象,并获取上传的文件
HttpPostedFile file = FileUpload1.PostedFile;
//获取上传文件的文件名和扩展名
string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
string extension = Path.GetExtension(filename);
//实例化一个byte数组,其长度等于上传文件的长度
byte[] imagetype = new byte[file.ContentLength];
//将文件数据读取到byte数组中
file.InputStream.Read(imagetype, 0, file.ContentLength);
//判断图片格式
if ((extension == ".jpg") || (extension == ".png") || (extension == ".gif") || (extension == ".bmp"))
{
//表里写入数据
using (SqlConnection connection = new SqlConnection("Data Source=AFOD3-609221015;Initial Catalog=MyData;Integrated Security=True"))
{
connection.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = connection;
string commandText = "Insert into Images values (@image, @Rollno,@img,getdate())";
cmd.CommandText = commandText;
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add("@image", SqlDbType.VarBinary);
cmd.Parameters["@image"].Value = imagetype;
cmd.Parameters.Add("@Rollno", SqlDbType.VarChar);
cmd.Parameters["@Rollno"].Value = txtrollno.Text;
cmd.Parameters.Add("@img", SqlDbType.VarChar);
cmd.Parameters["@img"].Value = txtrollno.Text;
cmd.ExecuteNonQuery();
cmd.Dispose();
connection.Close();
Response.Write("导入成功");
}
}
else
{
Response.Write("导入失败"); return;
}
}
using System; using System.Configuration; using System.Data.SqlClient; using System.IO;
protected void Button1_Click(object sender, EventArgs e)
{
string sConn = ConfigurationManager.AppSettings["ConnectionString"];
SqlConnection objConn = new SqlConnection(sConn);
objConn.Open();
string sql = "select * from Images";
SqlCommand cmd = new SqlCommand(sql, objConn);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
byte[] bytes = (byte[])dr["img"];
FileStream fs = new FileStream(@"E:\Images\" + dr["roll_no"] + ".jpg" , FileMode.Create, FileAccess.Write);
fs.Write(bytes, 0, bytes.Length);
fs.Flush();
fs.Close();
}
dr.Close();
objConn.Close();
Response.Write("成功导出");
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有