C# 存放二进制图片到数据库表

luenshan 12年前

    string name = FileUpload1.PostedFile.FileName;
        string type=name .Substring (name .LastIndexOf (".")+1);
        FileStream fs = File.OpenRead(name);
        byte[] content=new  byte [fs.Length];
        fs.Read(content, 0, content.Length);
        fs.Close();
        SqlConnection cn = new SqlConnection("server=.;uid=sa;pwd=****;database=za");
        SqlCommand cm = new SqlCommand("insert into myimage(imagedata) values(@imagedata)",cn);
        cn.Open();
        if (type == "jpg" || type == "gif" || type == "bmp" || type == "png")
        {
            cm.Parameters.Add("@imagedata", SqlDbType.Image);
            cm.Parameters["@imagedata"].Value = content;
            cm.ExecuteNonQuery();
            cn.Close();
        }

        Response.Write("保存成功");