求一段vb.net中,存储、读取ACCESS数据库中图片信息的代码。
求一段vb.net中,存储、读取ACCESS数据库中图片信息的代码要的是winform中的代码,越详细越好,谢谢。 --------------------编程问答-------------------- //得到提交的文件
Stream fileDataStream = MyFile.PostedFile.InputStream;
//得到文件大小
int fileLength = MyFile.PostedFile.ContentLength;
//创建数组
byte[] fileData = new byte[fileLength];
//把文件流填充到数组
fileDataStream.Read(fileData,0,fileLength);
//得到文件名字
string fileTitle = MyFileName.Value;
//得到文件类型
string fileType = MyFile.PostedFile.ContentType;
//构建数据库连接,SQL语句,创建参数
string strCnn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("Image2Access.mdb");
OleDbConnection myConnection = new OleDbConnection(strCnn);
OleDbCommand command = new OleDbCommand ("INSERT INTO Person (PersonName,PersonEmail,PersonSex,PersonImageType,PersonImage)" +
"VALUES (@PersonName,@PersonEmail,@PersonSex,@PersonImageType,@PersonImage)", myConnection);
System.Data.OleDb.OleDbParameter paramPersonName = new OleDbParameter("@PersonName", System.Data.OleDb.OleDbType.VarChar,50);
paramPersonName.Value = fileTitle;
command.Parameters.Add(paramPersonName);
System.Data.OleDb.OleDbParameter paramPersonEmail = new OleDbParameter("@PersonEmail", System.Data.OleDb.OleDbType.VarChar,50);
paramPersonEmail.Value = "mengxianhui@dotnet.aspx.cc";
command.Parameters.Add(paramPersonEmail);
System.Data.OleDb.OleDbParameter paramPersonSex = new OleDbParameter("@paramPersonSex", System.Data.OleDb.OleDbType.VarChar,50);
paramPersonSex.Value = "男";
command.Parameters.Add(paramPersonSex);
System.Data.OleDb.OleDbParameter paramPersonImageType = new OleDbParameter("@PersonImageType", System.Data.OleDb.OleDbType.VarChar,50);
paramPersonImageType.Value = fileType;
command.Parameters.Add(paramPersonImageType);
System.Data.OleDb.OleDbParameter paramPersonImage = new OleDbParameter("@PersonImage", System.Data.OleDb.OleDbType.Binary);
paramPersonImage.Value = fileData;
command.Parameters.Add(paramPersonImage);
//打开连接,执行查询
myConnection.Open();
command.ExecuteNonQuery();
myConnection.Close();
--------------------编程问答--------------------
--------------------编程问答-------------------- Dim conn As New SqlClient.SqlConnection
Dim fs As New IO.FileStream("D:\a.txt", IO.FileMode.OpenOrCreate, IO.FileAccess.Read) '拿地址存
Dim MyData As Byte() = New Byte(fs.Length - 1) {}
fs.Read(MyData, 0, System.Convert.ToInt32(fs.Length))
fs.Close()
Dim cmd As New SqlClient.SqlCommand("insert into Cr_m_File_dta([CR5_FILE_DATA]) values (@image)", g_SqlConn)
With cmd
.CommandType = CommandType.Text
.Parameters.Add("@image", SqlDbType.Image).Value = MyData
.ExecuteNonQuery()
End With
Dim SQLtile As String
Dim fs As IO.FileStream
数据库链接:
Dim strConn As String = "data source=127.0.0.1;initial catalog=hhdata;user id=sa;password=1234567890;"
conn = New SqlClient.SqlConnection(strConn)
conn.Open()
打开图片:
Dim Opendlg As New OpenFileDialog
Opendlg.Title = "Select a picture file"
Opendlg.Filter = "(*.bmp)|*bmp|(*.jpg)|*.jpg"
Opendlg.ShowDialog()
TextBox2.Text = Opendlg.FileName.ToString()
fs = New IO.FileStream(Opendlg.FileName.ToString(), IO.FileMode.Open, IO.FileAccess.Read)
保存图片:
Dim sqltile As String
sqltile = "INSERT INTO employees (lastname,firstname,photo) VALUES (@lastname,@firstname,@photo)"
Dim cmdkh As New SqlClient.SqlCommand(sqltile, conn)
Dim Data(fs.Length) As Byte
fs.Read(Data, 0, Int(fs.Length))
Dim prm1 As New SqlClient.SqlParameter("@lastname", TextBox2.Text)
Dim prm2 As New SqlClient.SqlParameter("@firstname", TextBox2.Text)
Dim prm3 As New SqlClient.SqlParameter("@photo", SqlDbType.VarBinary, Int(fs.Length), ParameterDirection.Input, False, 0, 0, "", DataRowVersion.Current, Data)
cmdkh.Parameters.Add(prm1)
cmdkh.Parameters.Add(prm2)
cmdkh.Parameters.Add(prm3)
cmdkh.ExecuteNonQuery()
fs.Close()
提取显示图片:
Dim bh As New DataSet
Dim dabh As New SqlClient.SqlDataAdapter
SQLtile = "select * from employees"
dabh.SelectCommand = New SqlClient.SqlCommand(SQLtile, conn)
dabh.Fill(bh)
Dim data() As Byte = bh.Tables(0).Rows(0)("photo")
Dim stmphoto As New IO.MemoryStream(data)
PictureBox1.Image = Image.FromStream(stmphoto)
以上代码在VB2005上测试通过
注:photo的数据类型是 image
转来的,希望对你有用~
补充:.NET技术 , VB.NET