首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

C#获取远路图片并以二进制保存到数据库

2012-08-22 
C#获取远程图片并以二进制保存到数据库。web应用程序已知远程图片地址以byte[]保存到数据库[解决办法]1、用W

C#获取远程图片并以二进制保存到数据库。
web应用程序
已知远程图片地址
以byte[]保存到数据库



[解决办法]
1、用WebClient。DownloadFile下载

2、保存

C# code
/// <summary>        /// 保存图片内容        /// </summary>        /// <param name="strName">图片名称</param>        /// <param name="strPath">图片路径</param>        /// <param name="strDescription">图片说明</param>        /// <param name="strImage">图片内容</param>        private void SaveTable(string strName, string strPath,string strDescription,byte[] strImage)        {            try            {                using (SqlConnection conn = new SqlConnection("Data Source=192.168.0.21;Initial Catalog=Book Keeping;User ID=sa;Password=sa;Connection Timeout=60;"))                {                    conn.Open();                    SqlCommand comm = new SqlCommand();                    string strSql = "Insert into PictureInfo(strName,strPath,strDescription,strImageContent) values (@Name,@Path,@Description,@Image)";                    comm.CommandText = strSql;                    comm.CommandType = CommandType.Text;                    comm.Connection = conn;                    comm.Parameters.Add("@Name",SqlDbType.VarChar);                    comm.Parameters.Add("@Path",SqlDbType.VarChar);                    comm.Parameters.Add("@Description",SqlDbType.VarChar);                    comm.Parameters.Add("@Image",SqlDbType.Image);                    comm.Parameters[0].Value = strName;                    comm.Parameters[1].Value = strPath;                    comm.Parameters[2].Value = strDescription;                    comm.Parameters[3].Value = strImage;                    comm.ExecuteNonQuery();                }            }            catch(Exception)            {            }        } 

热点排行