/// <summary>
/// 将照片转换为二进制数组
/// </summary>
/// <param name=&#34;path&#34;></param>
/// <returns></returns>
private byte[] PhotoToArray( string path )
{
FileStream stream = new FileStream( path , FileMode.Open , FileAccess.Read ) ;
byte[] bufferPhoto =new byte[stream.Length] ;
stream.Read( bufferPhoto,0,Convert.ToInt32( stream.Length ) ) ;
stream.Flush();
stream.Close();

return bufferPhoto ;
}

//把二进制的图片插到数据库
private void Save(byte[] image)
{
string sql = &#34;ins&#101;rt into table2(aaa,photo) values(@aaa,@photo)&#34;;
SqlParameter[] param=new SqlParameter[2];
param[0] =new SqlParameter(&#34;@aaa&#34;,SqlDbType.Int);
param[0].Value = 1;
param[1]= new SqlParameter(&#34;@photo&#34;,SqlDbType.Image);
param[1].Value= image;

SqlConnection conn= new SqlConnection();
conn.ConnectionString = System.Configuration.ConfigurationSettings.AppSettings[0];

SqlCommand commd= new SqlCommand(sql,conn);
commd.Parameters.Add(param[0]);
commd.Parameters.Add(param[1]);

try
{
conn.Open();
commd.ExecuteNonQuery();
MessageBox.Show(&#34;把图片成功的插入数据库&#34;);
}
catch(Exception ex)
{
throw ex;
}
finally
{
conn.Close();
}
}

引用地址:https://www.dfbys.cn/archives/54.html

  留言暂时关闭