图片二进制存入数据库

来源:互联网 发布:定义存贮字符串的数组 编辑:程序博客网 时间:2024/04/30 00:24
int code = int.Parse(this.TextBox1.Text);//图片编码        string value = this.FileUpload1.PostedFile.FileName.ToString();//图片路径        string type = value.Substring(value.LastIndexOf(".") + 1);        FileStream fs = File.OpenRead(value);        byte[] content = new byte[fs.Length];        fs.Read(content, 0, content.Length);        fs.Close();        string sqlconfrom = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["sqlConnectionStrings"].ToString();        SqlConnection con = new SqlConnection(sqlconfrom);        string sql = " insert into Image_Table values ( '1',@content,@code ) ";        SqlCommand cmd = new SqlCommand(sql, con);        cmd.Parameters.Add(new SqlParameter("@content", SqlDbType.Image));        cmd.Parameters["@content"].Value = content;        cmd.Parameters.Add(new SqlParameter("@code", SqlDbType.Int));        cmd.Parameters["@code"].Value = code;        con.Open();        int result = cmd.ExecuteNonQuery();        if (result > 0)        {            Response.Write("插入成功!");        }        con.Close();


0 0
原创粉丝点击