???? ID: 317701 - ????? ???????: 04 ?????? 2010 - ??????: 2.0
TO HOW: ????? ????????? ???? ????? ???? ??????? ?? ???? ????? C# ?? ??? ??? PictureBox ???????? ???? ?? ???
?????? ?????? This article applies to a different operating system than the one you are using. Article content that may not be relevant to you is disabled.
?? ??? ?? ??? ???? ????? ?? ?? ???? ??? ???? ???? ??????? ??? ???????? ??? ?? ????????? ????? ?? ??? ???
PictureBox ??? ?? ???? ????? ?? ?????? ?? ??? ???? ???? ???? Windows ??????? ?? ?????????
Microsoft Visual Basic 6.0, ?? ??????? ??? ?? ???? ??? ?? ????????? ???? ?? ??? ??????? ????? ??? ??
PictureBox ??????? ??? ??????? ??? ???????? ?? ?????? ?? ???? ???????? {?????) ????? ???? ?? ??? ???? ???? ??? ???? ??
PictureBox ??? ???? ????? ?? ???? ActiveX ???? ???????? (ADO) ???? ???????? ?? Recordset. ????????? ??? ???? ?? ??? ??? ????? ???????? ??? LoadPicture ??? ?? ?????? ????? ?? ??? ???? ????? ?? ??? ??? ?? ????? ???? (???? ???? ????????) ?? ??? ????? ???? ???
?? ???? ??? ?? ?? ????? ?????
MemoryStream ?? ????????
System.IO ???? ??? ??????? ?? ??? ???? ?? ????????? ????? ?? ??? ???? ??????
PictureBox ???????? ???
??????????????? ???? ??? ???????? ?????????, ??????????, ??????? ??????, ?? ?????? ??? ?? ???????? ???? ?? outlines ??:
Microsoft Visual Studio .NET ???? Microsoft Windows ???????? ?????? ?? ??????? ???? ?????? Microsoft SQL Server ?? ??????? ?? ??? ?????? Microsoft Access ??????? ?? ??????? ?? ???? ????? ?? ?? ?? ????? ???? ?? ?????? ???:
C# .NET Windows ??????? ?? ????? ????????? ??????? ??? ?????? ??? ???????? (?????) ??????? ADO.NET ???? ????? ?????SQL ????? ?? Access ?????? ?? ????? ?????? ??? ?????:
CREATE TABLE BLOBTest
(
BLOBID INT IDENTITY NOT NULL,
BLOBData IMAGE NOT NULL
)
Visual Studio .NET ?????, ?? ??? ??? ????? C# Windows ????????? ????????? ?????? ????? ???PictureBox ?? ????? ???????? ??????? Form1 ?? ?????? ??? ??????? ???? ?? ??? Button1 ?? ?????????? ?? ??? ????? ????? ???? ?? ??? Button2 ?? ?????????? PictureBox ???? ?? ??? . ????? ???????? ?????? ????? ???? ??????? ?? ??? ??????? ?? ????? ?? ???:
using System.Data.SqlClient;
using System.IO;
using System.Drawing.Imaging;
??????? ??????? ???????? ?? ??? ????? declaration ?? ???? inside ??????Form1 ????????? ????: System.Windows.Forms.Form declaration ???? ?? ??????? ???????? ?? ?????? ?? ??? ??? ???????? ????:
String strCn = "Data Source=localhost;integrated security=sspi;initial catalog=mydata";
Button1 (?? ????????? ????? ????? ??? ????? ??? ???????? ??????????? ?? ??? ????? ). ????? ?? ?? ?????? ?? ??? ??? ?????? ????? ??? ????? ?? ??? ???????? ????? ????? ?? ?? ??? ???? ??? ????? (?? ????? ?? ????FileStream ????????) ??? ?????? ????, ?? ???? ??? ?? parameterized ?? ????? ?? ??????? ??? ???? ???????? ???? ?????? ???????? ???
try
{
SqlConnection cn = new SqlConnection(strCn);
SqlCommand cmd = new SqlCommand("INSERT INTO BLOBTest (BLOBData) VALUES (@BLOBData)", cn);
String strBLOBFilePath = @"C:\blue hills.jpg";//Modify this path as needed.
//Read jpg into file stream, and from there into Byte array.
FileStream fsBLOBFile = new FileStream(strBLOBFilePath,FileMode.Open, FileAccess.Read);
Byte[] bytBLOBData = new Byte[fsBLOBFile.Length];
fsBLOBFile.Read(bytBLOBData, 0, bytBLOBData.Length);
fsBLOBFile.Close();
//Create parameter for insert command and add to SqlCommand object.
SqlParameter prm = new SqlParameter("@BLOBData", SqlDbType.VarBinary, bytBLOBData.Length, ParameterDirection.Input, false,
0, 0, null, DataRowVersion.Current, bytBLOBData);
cmd.Parameters.Add(prm);
//Open connection, execute query, and close connection.
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
}catch(Exception ex)
{MessageBox.Show(ex.Message);}
Button2 (?? ????????? ????? ????? ??? ????? ??? ???????? ??????????? PictureBox ???? ?? ??? ). ?? ??? retrieves ?? ?????????BLOBTest ?????? ??? ??????? ??? ???DataSet , ???? ??? ?? ??? ???? ??? ??? ???????????? ???????? ???? ?? ???? ??? ??? ??MemoryStream ????????, ?? ???? ??? loadsMemoryStream ?????? ?? ???PictureBox ???????? ???
try
{
SqlConnection cn = new SqlConnection(strCn);
cn.Open();
//Retrieve BLOB from database into DataSet.
SqlCommand cmd = new SqlCommand("SELECT BLOBID, BLOBData FROM BLOBTest ORDER BY BLOBID", cn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "BLOBTest");
int c = ds.Tables["BLOBTest"].Rows.Count;
if(c>0)
{ //BLOB is read into Byte array, then used to construct MemoryStream,
//then passed to PictureBox.
Byte[] byteBLOBData = new Byte[0];
byteBLOBData = (Byte[])(ds.Tables["BLOBTest"].Rows[c - 1]["BLOBData"]);
MemoryStream stmBLOBData = new MemoryStream(byteBLOBData);
pictureBox1.Image= Image.FromStream(stmBLOBData);
}
cn.Close();
}
catch(Exception ex)
{MessageBox.Show(ex.Message);}
?????? ???? ?? ????????? ?? ????? ?? ??? F5 ?????? ????? ??????????? ?? ??? ????? ??????? ??? ?? ?? ?? ?? ????? ??? ?? ??? ???? ?? ??? ??? ??? ????? ??????????? PictureBox ???? ?? ??? ??? ??? ??? ????? ?? ????????? ???? ?? ???PictureBox ???????? ??? ??? ?? ?? ????? ???????? ???? ?? ??? ????? ???? ????? ???PictureBox ??????? ??? ????????, ???? ????? ????????? ????????? ???? ?? ???? ????? ????? ????????? ??? ????? ??? ???????? ????? ?? ??? retrieves ?? ??? ????PictureBox ??? ????????? ???MemoryStream ????????, ?????????MemoryStream ??? ??????? ????, ?? ?? ?????????? ??????? ?? ???? parameterized ?? ????? ???? ?? ??? ???????? ???????? ???
try
{
SqlConnection cn = new SqlConnection(strCn);
SqlCommand cmd = new SqlCommand("INSERT INTO BLOBTest (BLOBData) VALUES (@BLOBData)", cn);
//Save image from PictureBox into MemoryStream object.
MemoryStream ms = new MemoryStream();
pictureBox1.Image.Save(ms, ImageFormat.Jpeg);
//Read from MemoryStream into Byte array.
Byte[] bytBLOBData = new Byte[ms.Length];
ms.Position = 0;
ms.Read(bytBLOBData, 0, Convert.ToInt32(ms.Length));
//Create parameter for insert statement that contains image.
SqlParameter prm = new SqlParameter("@BLOBData", SqlDbType.VarBinary, bytBLOBData.Length, ParameterDirection.Input, false,
0, 0,null, DataRowVersion.Current, bytBLOBData);
cmd.Parameters.Add(prm);
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
}catch(Exception ex)
{MessageBox.Show(ex.Message);}
????????? ?? ?????? ????? ??????????? PictureBox ???? ?? ??? ??? ???? ?? ????? ?? ???? ??? ?? ????????? ???? ?? ??? ???PictureBox ???????? ??? ?? ??? ?????? ?? ??? ???? ?? ??? ?? ????? ????PictureBox ??? ???????? ???? ??? ????? ??????????? PictureBox ???? ?? ??? ??? ??? ??? ??? ?? ????? ?? ?? ?????? ?? ??? ??? Pitfalls?? ???? ?? ????? ???? ?????????? ???????????????? ????? ??????????????? ??????? ?? Access ?? SQL ????? ?? ??? ?????? ???? ???? ??? ?????? ??? ??? ???????? ????????? ????? ????? ??? ??????? Visual Basic 6.0 OLE ???????? ???????? ?????? ???? ?? ?? wrapped ?? ???? ??? ?? ?? ??? ?? ??????? ???? ?? ??? ???? Access ??????? ?? ????? ???? ?? ???????? ??, ?? ???? ???? ?????? ?? ??? ??? Access ?????? ??? ????? ????? ?? ???OLE ???????? , ?? ?? ????? ????System.Data.OleDb ??? ????? ?? ????? ?? Microsoft Jet 4.0 ??????? ?? ???System.Data.SqlClient ??? ????? ??? ????? C# .NET ?? ??? ????? ???? ?? ????? ???? ?? ???? ??? ???????? ??????? ?? ??? ?????? ?? Microsoft ???????? ??? ????? ?? ??? ????? ???? ???????? ?? ????? ????:
309158
(http://support.microsoft.com/kb/309158/
)
TO HOW: ????? ?? ????? ???? ?? ????? C# .NET ?? ??? ADO.NET ?? ????? ?? ?????
317016
(http://support.microsoft.com/kb/317016/
)
????? ?? ????? ???? ????? ?? ??? ?? ???? ????? ????? ?? ADO.NET ?? ????? C# .NET ?? ????? ?? ???? ????
???? ???? ???? ??: Microsoft ADO.NET 1.1 Microsoft Visual C# .NET 2002 Standard Edition Microsoft Visual C# .NET 2003 Standard Edition kbdatabinding kbhowtomaster kbmt KB317701 KbMthi
???? ?????? ???????? ??????????: ?? ???? ?? ???? ??????? ?? ????? ?? Microsoft ????-?????? ?????????? ?????? ?????? ???? ??? ??. Microsoft ???? ??? ????-???????? ?? ????-???????? ????? ?????? ?? ???? ???????? ???? ?? ???? ????? ????? ??? ?? ??? ?????? ?? ???? ???? ???? ??? ????? ??. ???????, ????-???????? ???? ????? ???? ???? ???? ???. ?????, ????????, ?????-???? ?? ??????? ?? ???????? ?? ???? ???, ???? ?? ??? ?????? ???? ???? ??? ????? ??? ?? ???? ??. Microsoft ??????? ??? ???? ?? ?????? ?? ??????????, ????????? ?? ??? ?????? ?? ???? ????? ?? ???? ???????? ?? ??? ???? ????? ?? ??? ????????? ???? ??. Microsoft ????-?????? ?????????? ?? ????? ?????? ?? ?? ??? ??.
?????????? ?? ??????? ????????? ??????? ??:
317701
(http://support.microsoft.com/kb/317701/en-us/
)
?? ??????? ?? ??????????? ???
???? ?? ??????? ?? ???? ?????? ?? ?? ?????
???? ?? ??????? ??????? ???
???? ?? ???? ?? ????? ???? ?? ??? ????????? ??? ?? ????? ?????? ?????
?? ??????? ?? ????? ???? ?? ??? ?? ???? ?? ???? ????
???? ???????? ?? ????? ?? ???, ???? ??????????? ??? ?????? ??????? ????? ? ????.
???????! ????? ?????? ??????? ?? ????? ??? ??? ?? ??? ???? ??????????? ?? ????? ???? ???? ??. ???? ?????? ???????? ?? ???, ?????
??? ?? ?????? ??? ????? ?? ????.
???? ?????? ???? ?????? ??????
??????
??? ?????? ??????? ????
???? ??????