如何在sql server如何保存和提取图片
现有在sql server中有表student,字段为studentid(int),Name(varchar), photo(image),请问如何在表中保存和提取图片等。老师要求保存和提取该数据表中的信息(包括图片)全部用存储过程来实现,在WINFORM下开发,C/S形式,高手帮们给段代码吧!小弟初学C#,给段详细代码过程小弟拜谢了!!!!
问题点数:0、回复次数:7Top
1 楼drk928(一起看斜阳)回复于 2005-06-02 08:55:07 得分 0
上传图片
If OpenFileDialog1.ShowDialog() = DialogResult.Cancel Then
Exit Sub
End If
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
With PictureBox1
.Image = Image.FromFile(OpenFileDialog1.FileName)
.SizeMode = PictureBoxSizeMode.CenterImage
.BorderStyle = BorderStyle.Fixed3D
End With
End If
Dim ms As New MemoryStream()
PictureBox1.Image.Save(ms, PictureBox1.Image.RawFormat)
Dim arrImage() As Byte = ms.GetBuffer
ms.Close()
Dim imgid As String = CInt(Int(10 * Rnd()) + Int(100 * Rnd()) + Int(1000 * Rnd()) + Int(10000 * Rnd()) + Int(100000 * Rnd()) + Int(1000000 * Rnd()) + Int(10000000 * Rnd())).ToString
Try
Dim conn As New GWConnection()
Dim northwindConnection As New SqlConnection()
northwindConnection = conn.GetCN()
Dim strSQL As String = "INSERT INTO t2_005(imgid,pactcode,Picture) VALUES(@imgid,@pactcode,@Picture)"
Dim cmd As New SqlCommand(strSQL, northwindConnection)
With cmd
.Parameters.Add(New SqlParameter("@imgid", SqlDbType.NVarChar, 50)).Value = imgid
.Parameters.Add(New SqlParameter("@pactcode", SqlDbType.NVarChar, 16)).Value = txt_pactcode.Text
.Parameters.Add(New SqlParameter("@Picture", SqlDbType.Image)).Value = arrImage
End With
If Not northwindConnection.State = ConnectionState.Open Then
northwindConnection.Open()
End If
cmd.ExecuteNonQuery()
northwindConnection.Close()
'txt_imgid.Text = imgid
Catch ex As SqlException
MsgBox("图片上传失败!", MsgBoxStyle.OKOnly + MsgBoxStyle.Exclamation, "出错提示!")
End Try
Top
2 楼bobseadream(bobo)回复于 2005-06-02 08:59:17 得分 0
// Source Code for Save the image file into the database
public void OnUpload(Object sender, EventArgs e)
{
// Create a byte[] from the input file
int len = Upload.PostedFile.ContentLength;
byte[] pic = new byte[len];
Upload.PostedFile.InputStream.Read (pic, 0, len);
// Insert the image and comment into the database
SqlConnection connection = new
SqlConnection (@"server=INDIA\INDIA;database=iSense;uid=sa;pwd=india");
try
{
connection.Open ();
SqlCommand cmd = new SqlCommand ("insert into Image "
+ "(Picture, Comment) values (@pic, @text)", connection);
cmd.Parameters.Add ("@pic", pic);
cmd.Parameters.Add ("@text", Comment.Text);
cmd.ExecuteNonQuery ();
}
finally
{
connection.Close ();
}
}
private void GetImage(string str)
{
int num=this.dataGrid1.CurrentRowIndex;
if(num==-1)
{
return;
}
if(dataset.Tables[0].Rows[num]["照片"]!=Convert.DBNull)
{
byte[] bytes=(byte[])dataset.Tables[0].Rows[num]["照片"];
MemoryStream memStream=new MemoryStream(bytes);
Bitmap bitmap = new Bitmap(memStream);
memStream.Close();
this.pictureBox1.Image = bitmap;
}
else
{
this.pictureBox1.Image = null;
}
}
Top
3 楼dan83(共想主义事业)回复于 2005-06-02 09:16:03 得分 0
学习ingTop
4 楼wang8712(Andy)回复于 2005-06-02 10:25:42 得分 0
你获取图片可以在数据库中保存数据库的路径,在程序中取就方便了.Top
5 楼zhbzspqxl()回复于 2005-06-03 07:54:44 得分 0
老师要求数据库中保存图片,我用的是C#,如何做呀!!!!Top
6 楼lyb_abiandbel(专注于OO分析与设计)回复于 2005-06-03 08:34:39 得分 0
添加图片:http://dotnet.aspx.cc/ShowDetail.aspx?id=2A5DD7C6-A45A-48AB-A2E8-342A29F17506
显示图片:http://dotnet.aspx.cc/ShowDetail.aspx?id=ECD9AE16-8FF0-4A1C-9B9F-5E8B641CB1B1
我用的就是这个,好用的。
Top
7 楼NetFishDuDu()回复于 2005-06-03 08:53:55 得分 0
搜索一下,有很多这样的例子,可能语言不同,但思路是一样的吧Top




