简单的将图片上传到数据库

作者在 2012-05-13 12:05:00 发布以下内容
首先在web.config中配置数据库连接对象具体如下:
双击打开web.config找到<appSettings/><connectionStrings/> <system.web>将原来的<connectionStrings/> 删除再添加<connectionStrings>此时有<connectionStrings>和</connectionStrings>在<connectionStrings>和</connectionStrings>添加如下:<add name="ConnString" connectionString="Data Source=.\SQLEXPRESS;Initial CataLog=student;User ID=linzai;PassWord=xiaolinzai;" providerName="System.Data.SqlClient"/>
字符串。简单说明一下它们的含义:name=“”(数据库连接字符串),connectionString="Data Source=.\SQLEXPRESS(连接服务器名)我这里是用本地的(.\SQLEXPRESS),Initial CataLog=数据库名,
ID=连接数据库的用户名,password=密码,providerName="System.Data.SqlClient“连接的数据库类型(我用得是sql数据库)
设置的数据库如下:
接下来新建web窗体在新建的web中添加using System.Data.SqlClient;
和using System.IO;命名空间
其代码如下:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.IO;
public partial class image : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected Int32 FileLength = 0;//记录文件长度变量
    protected void Button1_Click(object sender, EventArgs e)
    {
        HttpPostedFile UpFile = FileUpload1.PostedFile;//HttpPostedFile 对象用于读取图像
        FileLength = UpFile.ContentLength;//记录文件长度
        try
        {
            if (FileLength == 0)
            {
                Label1.Text = "<b>请选择要上传的图片</b>";
                return;
            }
            else
            {
                Byte[] Filebytearry = new Byte[FileLength];//图片临时存储
                Stream Streamobject = UpFile.InputStream;//建立数据流
                Streamobject.Read(Filebytearry, 0, FileLength);//读取图像文件数据,filebytearry为数据储存体,0为数据指针位置,filelength为数据长度
                string sqlstr = ConfigurationManager.ConnectionStrings["ConnString"].ToString();
                using (SqlConnection Con = new SqlConnection(sqlstr))
                {
                    // SqlConnection Con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnString"].ToString());
                     string sqlcmd = "INSERT INTO tImage(ImageDate,ImageContentType,ImageDescriiption,ImageSize) values(@Image,@ImageContentType,@ImageDescriiption,@ImageSize)";
                    SqlCommand cmd = new SqlCommand(sqlcmd, Con);
                    //cmd.Parameters.Add("@ImageStore", SqlDbType.Int).Value = Session["ImageStore"].ToString();
                    cmd.Parameters.Add("@Image", SqlDbType.Binary, FileLength).Value = Filebytearry;//记录图片信息
                    cmd.Parameters.Add("@ImageContentType", SqlDbType.VarChar, 50).Value = UpFile.ContentType;//记录图片类型
                    cmd.Parameters.Add("@ImageDescriiption", SqlDbType.VarChar, 200).Value = TextBox1.Text;//记录图片说明信息
                    cmd.Parameters.Add("@ImageSize", SqlDbType.BigInt, 8).Value = UpFile.ContentLength; ;//记录图片长度
                    Con.Open();
                    cmd.ExecuteNonQuery();
                    Con.Close();
                    Label1.Text = "<b>成功上传<b>";


                }
            }
        }
        catch (Exception ex)
        {
            Label1.Text = ex.Message.ToString();
        }
    }

}
默认分类 | 阅读 1143 次
文章评论,共0条
游客请输入验证码
文章分类