读取数据库图片后复制该图片的方法:
public void ReadPhoto(string photoFile)
{
string sql = "SELECT PHOTO FROM USERINFO WHERE UID=1";
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Server=.;database=Test;uid=sa;pwd=sa";
SqlCommand cmd = new SqlCommand(sql, conn);
try
{
conn.Open();
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.Read())
{
byte[] photo = sdr[0] as byte[];
FileStream fs = new FileStream(photoFile, FileMode.CreateNew);
fs.Write(photo, 0, photo.Length);
fs.Close();
}
sdr.Close();
}
catch (Exception ex)
{
throw;
}
finally
{
conn.Close();
}
}protected void btnReadPhoto_Click(object sender, EventArgs e)
{
SQL sql = new SQL();
sql.ReadPhoto(Server.MapPath("~/Images/aaa.jpg"));
}
错误:aaa.jpg图片已经存在,删除之后再运行也是报存在的错误,请问是怎么回事?谢谢!
public void ReadPhoto(string photoFile)
{
string sql = "SELECT PHOTO FROM USERINFO WHERE UID=1";
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Server=.;database=Test;uid=sa;pwd=sa";
SqlCommand cmd = new SqlCommand(sql, conn);
try
{
conn.Open();
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.Read())
{
byte[] photo = sdr[0] as byte[];
FileStream fs = new FileStream(photoFile, FileMode.CreateNew);
fs.Write(photo, 0, photo.Length);
fs.Close();
}
sdr.Close();
}
catch (Exception ex)
{
throw;
}
finally
{
conn.Close();
}
}protected void btnReadPhoto_Click(object sender, EventArgs e)
{
SQL sql = new SQL();
sql.ReadPhoto(Server.MapPath("~/Images/aaa.jpg"));
}
错误:aaa.jpg图片已经存在,删除之后再运行也是报存在的错误,请问是怎么回事?谢谢!
{
if (m_Conn == null)
{
m_Conn = new SqlConnection(m_strConn);
}
else if (m_Conn.State == ConnectionState.Closed)
{
m_Conn.Open();
}
} private void CloseConn()//关闭数据库
{
if (m_Conn.State == ConnectionState.Open)
{
m_Conn.Close();
}
} private void button1_Click(object sender, EventArgs e)
{
//实例化一个打开文件类
using (OpenFileDialog openFiledDialog = new OpenFileDialog())
{
//设置初始目录
openFiledDialog.InitialDirectory = "D:\\";
//文件筛选
openFiledDialog.Filter = "JPG文件|*.jpg|GIF文件|*.gif|PNG文件|*.PNG";
//设置筛选的索引
openFiledDialog.FilterIndex = 1;
//弹出对话框
if (openFiledDialog.ShowDialog() == DialogResult.OK)
{
//获取文件完整路径
m_strPathName = openFiledDialog.FileName;
this.label1.Text = m_strPathName;
//从指定的路径创建文件
Image img = Image.FromFile(m_strPathName);
//设置为背景图片
this.pictureBox1.BackgroundImage = img;
this.pictureBox1.BackgroundImageLayout = ImageLayout.Stretch;
}
}
}
private void button2_Click(object sender, EventArgs e)
{
try
{ //将图像读入到字节数组
FileStream fs = new FileStream(m_strPathName, FileMode.Open, FileAccess.Read);
//将信息写入流中
byte[] buffByte = new byte[fs.Length];
fs.Read(buffByte, 0, (int)fs.Length);
fs.Close();
fs = null; //建立Command命令,将信息写入数据库
OpenConn();
string strSQL = @"Insert into testdaxiang(infor,name) values(@img,@name)";
SqlCommand comm = new SqlCommand(strSQL, m_Conn);
//创建Parameter
comm.Parameters.Add("@img", System.Data.SqlDbType.Image);
comm.Parameters[0].Value = buffByte;
comm.Parameters.Add("@name", System.Data.SqlDbType.VarChar);
comm.Parameters[1].Value = m_strPathName.Substring(m_strPathName.LastIndexOf("\\") + 1);
int nResult = comm.ExecuteNonQuery();
if (nResult < 0)
{
MessageBox.Show("保存失败××", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
else
{
MessageBox.Show("保存成功……", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
CloseConn();
buffByte = null;
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
} //返回一个图片信息
private Image GetImageInf(string strSQL)
{
SqlCommand cmd = new SqlCommand(strSQL, m_Conn);
SqlDataReader reader = cmd.ExecuteReader();
reader.Read();
MemoryStream buf = new MemoryStream((byte[])reader[0]);
Image img = Image.FromStream(buf, true);
return img; }
}