读书人

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

发布时间: 2012-08-22 09:50:35 作者: rapoo

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)            {            }        } 

读书人网 >C#

热点推荐