日期:2014-05-16  浏览次数:20470 次

C#上传byte类型图片到数据库

public void SaveFile(byte[] data)
    {
       StringBuilder cb = new StringBuilder();
        SqlCommand cmd = new SqlCommand();
        SqlParameter parm = new SqlParameter("@NewsImage", SqlDbType.Image);
        parm.Value = data;
        cmd.Parameters.Add(parm);
        using (SqlConnection conn = new SqlConnection(CONN_STRING))
        {
            cb.Append("INSERT INTO NewsInfo(NewsImage) VALUES(@NewsImage)");
            conn.Open();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = cb.ToString();
            cmd.ExecuteNonQuery();
            conn.Close();
            
        }
    }
    private string CONN_STRING = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;