从数据库调用图片和保存图片

做一个学生课堂管理系统
其中当点一个学生名字的时候
可以从数据库中调用该学生的信息
现在有个问题是还要该学生的相片
我不知道怎样存贮和调用该学生的相片
SQL可以有这个功能吗
,ENT还有什么实现的秒招吗
急用
谢谢
谢谢
发表意见我用的上的都有分
再谢谢
---------------------------------------------------------------

从数据库读图片:
private void PicShow()//图片加载
{
bytPic=new byte[0];
bytPic=(byte[])this.dsDataResult.Tables["S10CPDA"].Rows[this.BindingContext[this.dsDataResult,"S10CPDA"].Position]["TX1"];
mstr=new MemoryStream(bytPic);
if(mstr.Length>0)
{
this.picbLoadPic.Image=Image.FromStream(mstr);
mstr.Flush();
}
往数据库写图片:

OpenFileDialog of=new OpenFileDialog();
FileStream fs;
of.Filter="Picture Files(.jpg) ¦.jpg &brvbarBmp(.bmp) ¦.bmp &brvbarAll Files(.) ¦*.*";
if(of.ShowDialog()==DialogResult.OK&&of.FileName!="")
{
fs=new FileStream(of.FileName,FileMode.Open,FileAccess.Read);
byte[] btyPic=new byte[(int)fs.Length];
fs.Read(btyPic,0,btyPic.Length);
try
{
this.picbLoadPic.SizeMode=PictureBoxSizeMode.Normal;
this.picbLoadPic.Image=Image.FromFile(of.FileName);
}
catch(Exception ImageExp)
{
MessageBox.Show(ImageExp.Message);
}
fs.Close();
System.Data.SqlClient.SqlCommand sqlUpdateCommand11=new System.Data.SqlClient.SqlCommand();
sqlUpdateCommand11.Connection = this.sqlCon;
sqlUpdateCommand11.CommandText = "Update S10CPDA set TX1=@TX where S10BH=@S10BH1";
sqlUpdateCommand11.Connection.Open();
sqlUpdateCommand11.Parameters.Add(new System.Data.SqlClient.SqlParameter("@S10BH1", System.Data.SqlDbType.VarChar, 30, "S10BH"));
sqlUpdateCommand11.Parameters["@S10BH1"].Value=this.txtProductID.Text;
sqlUpdateCommand11.Parameters.Add(new System.Data.SqlClient.SqlParameter("@TX", System.Data.SqlDbType.Binary,bytPic.Length, "TX1"));
sqlUpdateCommand11.Parameters["@TX"].Value=btyPic;
try{sqlUpdateCommand11.ExecuteNonQuery();}
catch(Exception exp)
{MessageBox.Show(exp.Message);}
this.sqlDataadapter.Update(this.dsDataResult,"S10CPDA");
this.sqlDataadapter.Fill(this.dsDataResult,"S10CPDA");
sqlUpdateCommand11.Connection.Close();
MessageBox.Show("图片插入成功");

}
}

---------------------------------------------------------------

楼上的方法也可以,不过好像在b/s下比较方便,在c/s中不是很方便.

可以参考MSDN上的例子

往数据里写图片:
http://msdn.microsoft.com/library/chs/default.asp?url=/library/CHS/cpguide/html/cpconwritingblobvaluestodatabase.asp

从数据库读图片:
http://msdn.microsoft.com/library/chs/default.asp?url=/library/CHS/cpguide/html/cpconobtainingblobvaluesfromdatabase.asp
---------------------------------------------------------------

'保存图片
'Add Button
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button2.Click
' To Insert Image
Dim st As New FileStream(OpenFileDialog1.FileName, FileMode.Open, FileAccess.Read)
Dim s As String = TextBox1.Text
Dim mbr As BinaryReader = New BinaryReader(st)
Dim buffer(st.Length) As Byte
mbr.Read(buffer, 0, CInt(st.Length))
st.Close()
InsertImage(buffer, s)
End Sub

'Function For Inserting in the Procdeure in the Database
Public Function InsertImage(ByRef buffer, ByVal str)
cn = New SqlClient.SqlConnection(SqlConnection1.ConnectionString)
cn.Open()
Dim cmd As New SqlClient.SqlCommand("sp_InsertPhoto", cn)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add("@name", SqlDbType.VarChar).value = TextBox1.Text
cmd.Parameters.Add("@image", SqlDbType.Image).value = buffer
cmd.ExecuteNonQuery()
MsgBox("Image inserted")
cn.Close()
End Function
---------------------------------------------------------------

'显示
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button3.Click
Dim i As String = InputBox("请输入名字:")
ShowImage(i)
End Sub
'Function to Display Image
Private Sub ShowImage(ByVal s As String)
cn = New SqlClient.SqlConnection(SqlConnection1.ConnectionString)
cn.Open()
Dim str As String = "SELECT photo FROM Photos WHERE name='" & s & "'"
Dim cmd As New SqlClient.SqlCommand(str, cn)
TextBox1.Text = s
Dim b() As Byte
b = cmd.ExecuteScalar()
If (b.Length > 0) Then
Dim stream As New MemoryStream(b, True)
stream.Write(b, 0, b.Length)
DrawToScale(New Bitmap(stream))
stream.Close()
End If
cn.Close()
End Sub

'Function to Create Instance For the Image From the Buffer
Private Sub DrawToScale(ByVal bmp As Image)
PictureBox1.Image = New Bitmap(bmp)
End Sub
---------------------------------------------------------------

从数据库读图片:
添加引用abodb

Sub show_pic()
Dim sm As new ADODB.Stream
If Not p1.Image Is Nothing Then '清空图片框资源
p1.Image.Dispose()
p1.Image = Nothing
End If
If ds.Tables("学生表").Rows(j)(i) Is Nothing Then Exit Sub '如果字段没有图片退出
sm.Type = ADODB.StreamTypeEnum.adTypeBinary
sm.Open()
sm.Write(ds.Tables("学生表").Rows(j)(i))'从数据库读图片
sm.SaveToFile("c:\tmp.jpg", ADODB.SaveOptionsEnum.adSaveCreateOverWrite) '把图片暂时存到c:
p1.Image = Image.FromFile("c:\tmp.jpg")
sm.Close()
End Sub

Published At
Categories with Web编程
Tagged with
comments powered by Disqus