如何样把图片存入数据库? 然后把图片读出来? SQL2005 和VS2008
怎么样把图片存入数据库? 然后把图片读出来? SQL2005 和VS2008
我用的是SQL2005 和VS2008 做的
怎么对图片进行操作 最后达到存入图片和显示图片的效果
有代码的给点代码 没得给点方法。。。 谢啦。。。。
------解决方案--------------------
我这个是用2003写的,你可以参考一下,方法都是差不多的、
private void WriteStream(string fileName)
{
FileStream fs = new FileStream(fileName,FileMode.Open,FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
byte[] b = new byte[fs.Length];
br.Read(b,0,b.Length-1);
br.Close();
SqlConnection Conn = new SqlConnection(strConnectText);
Conn.Open();
string mySelectQuery = "insert into tb_Pic(Pic) values (@imgdata)";
SqlCommand myCommand = new SqlCommand(mySelectQuery, Conn);
SqlParameter paramData = new SqlParameter( "@imgdata", SqlDbType.Image );
paramData.Value = b;
myCommand.Parameters.Add(paramData);
myCommand.ExecuteNonQuery();
Conn.Close();
}
private void ReadStream()
{
SqlConnection Conn = new SqlConnection(strConnectText);
Conn.Open();
SqlCommand Cmd = new SqlCommand("select Pic from tb_Pic",Conn);
byte[] b = (byte[])Cmd.ExecuteScalar();
if(b.Length>0)
{
try
{
MemoryStream ms = new MemoryStream(b,0,b.Length-1,true);
picBox.Image = Image.FromStream(ms,true);
ms.Close();
}
catch(Exception e)
{
MessageBox.Show(e.ToString());
}
}
Conn.Close();
}
------解决方案--------------------
我用的是SQL2005 和VS2008 做的
怎么对图片进行操作 最后达到存入图片和显示图片的效果
有代码的给点代码 没得给点方法。。。 谢啦。。。。
------解决方案--------------------
我这个是用2003写的,你可以参考一下,方法都是差不多的、
private void WriteStream(string fileName)
{
FileStream fs = new FileStream(fileName,FileMode.Open,FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
byte[] b = new byte[fs.Length];
br.Read(b,0,b.Length-1);
br.Close();
SqlConnection Conn = new SqlConnection(strConnectText);
Conn.Open();
string mySelectQuery = "insert into tb_Pic(Pic) values (@imgdata)";
SqlCommand myCommand = new SqlCommand(mySelectQuery, Conn);
SqlParameter paramData = new SqlParameter( "@imgdata", SqlDbType.Image );
paramData.Value = b;
myCommand.Parameters.Add(paramData);
myCommand.ExecuteNonQuery();
Conn.Close();
}
private void ReadStream()
{
SqlConnection Conn = new SqlConnection(strConnectText);
Conn.Open();
SqlCommand Cmd = new SqlCommand("select Pic from tb_Pic",Conn);
byte[] b = (byte[])Cmd.ExecuteScalar();
if(b.Length>0)
{
try
{
MemoryStream ms = new MemoryStream(b,0,b.Length-1,true);
picBox.Image = Image.FromStream(ms,true);
ms.Close();
}
catch(Exception e)
{
MessageBox.Show(e.ToString());
}
}
Conn.Close();
}
------解决方案--------------------
- C# code
//保存图片 protected void Button1_Click(object sender, EventArgs e) { if (FileUpload1.HasFile == false) { Label1.Text = "上载的文件不存在"; return; } HttpPostedFile file = FileUpload1.PostedFile; if (file.ContentLength <= 0) { Label1.Text = "上载的文件的内容为空,不能上载"; return; } ///获取文件的名称和扩展名 string vfilename = System.IO.Path.GetFileName(file.FileName); ///定义保存文件的二进制数据 byte[] data = new byte[file.ContentLength]; ///读取文件的二进制数据 file.InputStream.Read(data, 0, file.ContentLength); ///获取配置文件Web.config中的连接字符串 string conString = ConfigurationManager.ConnectionStrings["WEB2ASPNET2DBConnectionString"].ConnectionString; ///创建连接SQL Server数据库的SqlConnection对象 SqlConnection myCon = new SqlConnection(conString); string cmdText = "INSERT INTO [Files] ([Name],[Type],[Data])VALUES('" + vfilename + "','" + file.ContentType + "',@Data)"; SqlCommand myCmd = new SqlCommand(cmdText, myCon); ///添加SQL语句的参数 SqlParameter pData = new SqlParameter(); pData.ParameterName = "@Data"; pData.Value = data; pData.Direction = System.Data.ParameterDirection.Input; myCmd.Parameters.Add(pData); try { myCon.Open(); ///打开连接 myCmd.ExecuteNonQuery();///将数据库保存到数据库 Label1.Text = "上载文件:“" + vfilename + "” 成功。"; } catch (SqlException sqlex) { ///如果连接失败,则显示错误信息 Label1.Text = sqlex.Message; } finally { ///关闭已经打开的连接 if (myCon != null) { myCon.Close(); } } }