TO HOW: ????? ?? ????? ???? ????? ?? ??? ?? ?? ????? ????? ?? ADO.NET ?? ????? C++ .NET ?? ????? ????

???? ?????? ???? ??????
???? ID: 317017 - ?? ???????? ?? ?????? ??? ?? ?? ???? ???? ???? ??.
?? ???? ???????? ?? ???? ??? ??. ??? "???? ??" ???? ?? ?????????? ???? ??? ?? ?? ?? ??????? ???? ???? ?????.
??? ?? ??????? ???? | ??? ?? ??????? ????

?? ????? ??

??????

Use this step-by-step guide to read and write data to and from BLOB (LongVarBinary) columns in a database table.

???

This article contains examples for both the SqlClient and the OleDbDataAdapter????? The only differences, apart from the class names, are the connection strings and the declaration of SQL parameters. The technique for retrieving BLOB data is fundamentally the same.

TheTestrecord in the categories table does not exist. You will need to add a CategoryName named Test. ??? ???? ?? ???, Microsoft SQL Server ?????? ???????? ??? ????? ??? ?????????:
use Northwind
Insert into categories(categoryname) values ('Test') 
				
?? ????? ????? ?? ????? ???? ?? ??? ?? ??????? ?? ?? ??????? ?? ??????? ?? ??? ?? ????? ??????? ?? ??????? ?? ??????? ?? ??? SQL ?????? ???????? ??? ????? ???? ?????? ???, F5 ??????
use Northwind
delete from Categories where CategoryName = 'Test'
					

??????????

????? ?????? ?? ????? ???????? ?????????, ??????????, ??????? ??????, ???? ?? ????? ?? ?????? ??? ?? ???????? ????:
  • Microsoft Windows 2000 Professional, Windows 2000 ?????, Windows 2000 ????? ????? ?? Windows NT 4.0 ?????
  • Microsoft Visual Studio .NET
  • Microsoft SQL Server 7.0 ?? ??? ?? ???????
?? ???? ????? ?? ?? ?? ????? ???? ?? ?????? ???:
  • Visual Studio .NET
  • ADO.NET fundamentals ?? ????????

????????? ????? ?? ??? ??????

  1. Visual Studio .NET ?? ??????? ?????
  2. ????? .NET 2002, ??? ?? ??? ???????? C++ ????????? ??????

    ????? .NET 2003, ??? ??? ????? ????????? (.NET) ??????

    ????????? "BLOBReadWrite." ?? ???
  3. BLOBReadWrite.cpp ???? ??? ????? ?? ????? ??? ???????? Visual Studio .NET ?????? ????? ?? ?????? ??? ?? ???????? ?? ?????
       #include "stdafx.h"
       #using <mscorlib.dll>
       #using <System.dll>
       #using <System.Data.dll>
    
       using namespace System;
       using namespace System::Data;
       using namespace System::Data::SqlClient;
       using namespace System::Data::OleDb;
       using namespace System::IO;
    
       // Prototypes for functions that do the actual work.
       void File2SqlBlob(String *);
       void File2OleDbBlob(String *);
       void SqlBlob2File(String *);
       void OleDbBlob2File(String *);
    
       int direction(void);
       int reader (void);
    
       
    
       // This is the entry point for the application.
       #ifdef _UNICODE
       int wmain(void)
       #else
       int main(void)
       #endif
       {
            // File you will create from database.
            String *DestFilePath = "c:\\mytest.bmp"; 
            // File you will insert into database.
            String *SourceFilePath = "c:\\windows\\coffee bean.bmp"; 
    
    	// Insert BLOB into database from file using SQL provider.
    	File2SqlBlob(SourceFilePath); 
    
    	// Insert BLOB into database from file using OleDb provider.
    	File2OleDbBlob(SourceFilePath);
    
    	// Read BLOB from database into file using SQL provider.
    	SqlBlob2File(DestFilePath);
    
    	// Read BLOB from database into file using OleDb provider.
            OleDbBlob2File(DestFilePath); 
    
       	return 0;   
       }
    					
  4. ?????? ?? ????????? ???????? ????? ????? ?? ????? ?? ??? ??????????????? ???? ?? ??? ??? ????? ????? ??? ??????? ??? ???????? ??? ?? ???? ?? BLOBReadWright.cpp ????? ?? ????? ??? ??? ????????
  5. ??? ?? ????????? ???? ?? ??? CTRL + F5 ?????? ????? ?? ???????? ????? ?? ????? ?? ?????? ???? ?? ?????? ????? ??? ????? ?????

??? ????? ????? ?? ????? ?? ??? ??????

????? ??? ?? ????? ???? ??DataReader????? ??? ??????? ???? ?? ??? ???? ?? ??? ????? ???? ?? ??? ??????????? ??? Because the BLOB is in memory, you do not need to chunk the data; it is assigned to a???????? ??? ???? ?? ??? ?? ??? ???GetBytes?????? ??? Rhe first call gets the length of the BLOB in bytes and allocates the???????? ??? The second call retrieves the data. TheFileStreamobject writes the????array to disk.
void SqlBlob2File(String *DestFilePath)
   {
	try{
        // The column number of the BLOB field.
        int PictureCol = 0; 
	SqlConnection *cn = new SqlConnection("server=localhost;integrated security=yes;database=NorthWind");
	SqlCommand *cmd = new SqlCommand("SELECT Picture FROM Categories WHERE CategoryName='Test'", cn);
	cn->Open();

	// Create server-side DataReader to read BLOB from database.
	SqlDataReader *dr  = cmd->ExecuteReader();
	dr->Read();

	// Create buffer for BLOB and read from DataReader. Close 
        // DataReader and Connection.
	Byte b[] = __gc new  Byte[Convert::ToInt32((dr->GetBytes(PictureCol, 0, 0, 0, Int32::MaxValue)))];
	dr->GetBytes(PictureCol, 0, b, 0, b->Length);
	dr->Close();
	cn->Close();

	// Open FileStream and write buffer to file.
	FileStream *fs = new FileStream(DestFilePath, FileMode::Create, FileAccess::Write);
	fs->Write(b, 0, b->Length);
	fs->Close();

	Console::WriteLine("SqlBlob2File completed successfully.\nPress return to continue.");
	Console::ReadLine();		
	}catch(SqlException *ex)
	 {Console::Write(ex->Message);}
   }

   void OleDbBlob2File(String *DestFilePath)
   {
	try{
         // The column number of the BLOB field.
	 int PictureCol = 0; 
	OleDbConnection *cn = new OleDbConnection("provider=SQLOLEDB;server=localhost;user id=user;password=pass;database=NorthWind");
	OleDbCommand *cmd = new OleDbCommand("SELECT Picture FROM Categories WHERE CategoryName='Test'", cn);
	cn->Open();

	// Create server-side DataReader to read BLOB from database.
	OleDbDataReader *dr  = cmd->ExecuteReader();
	dr->Read();

	// Create buffer for BLOB and read from DataReader. Close 
        // DataReader and Connection.
	Byte b[] = __gc new  Byte[Convert::ToInt32((dr->GetBytes(PictureCol, 0, 0, 0, Int32::MaxValue)))];
	dr->GetBytes(PictureCol, 0, b, 0, b->Length);
	dr->Close();
	cn->Close();

	// Open FileStream and write buffer to file.
	FileStream *fs = new FileStream(DestFilePath, FileMode::Create, FileAccess::Write);
	fs->Write(b, 0, b->Length);
	fs->Close();
	Console::WriteLine("OleDbBlob2File completed successfully.\nPress return to continue.");
	}catch(OleDbException *ex)
	 {Console::Write(ex->Message);}
   }
				

Functions to Write to a BLOB Column

????? ??? ?? ????? ???? ??????, ??????????objects to write data from a????array to a BLOB column. Because it reads the file into memory, it does not need to chunk the data when it writes to the server. The????array and its length are passed to the????????constructor.
void File2SqlBlob(String *SourceFilePath)
   {
	try{
	SqlConnection *cn = new SqlConnection("server=localhost;integrated security=yes;database=NorthWind");
	// Create SQL command containing @Picture parameter for BLOB.
	SqlCommand *cmd = new SqlCommand("UPDATE Categories SET Picture=@Picture WHERE CategoryName='Test'", cn);
			
	// Read FileStream into buffer and then close stream.
	FileStream *fs = new FileStream(SourceFilePath, FileMode::Open, FileAccess::Read);
	int size = Convert::ToInt32(fs->Length);
	Byte b[] = __gc new  Byte[size];		
	fs->Read(b, 0, size);
	fs->Close();

	// Create parameter for the @Picture contained in SQL statement.
	SqlParameter *P = new SqlParameter("@Picture", SqlDbType::VarBinary, b->Length, ParameterDirection::Input, false, 0, 0, 0, DataRowVersion::Current, b);
	cmd->Parameters->Add(P);

	// Open connection, execute query, and close connection.
	cn->Open();
	if (cmd->ExecuteNonQuery() == 1)		
	cn->Close();

	Console::WriteLine("File2SqlBlob completed successfully.\nPress return to continue.");
	Console::ReadLine();
	}catch(SqlException *ex)
	 {Console::Write(ex->Message);}
   }

   void File2OleDbBlob(String *SourceFilePath)
   {
	try{
	OleDbConnection *cn = new OleDbConnection("provider=SQLOLEDB;user id=user;password=pass;database=NorthWind");
	// Create SQL command containing ? parameter for BLOB.
	OleDbCommand *cmd = new OleDbCommand("UPDATE Categories SET Picture=? WHERE CategoryName='Test'", cn);
	
	// Read FileStream into buffer and then close stream.
	FileStream *fs = new FileStream(SourceFilePath, FileMode::Open, FileAccess::Read);
	int size = Convert::ToInt32(fs->Length);
	Byte b[] = __gc new  Byte[size];		
	fs->Read(b, 0, size);
	fs->Close();
	
	// Create parameter for the ? contained in the SQL statement.
	OleDbParameter *P = new OleDbParameter("@Picture", OleDbType::VarBinary, b->Length, ParameterDirection::Input, false, 0, 0, 0, DataRowVersion::Current, b);
	cmd->Parameters->Add(P);
	
	// Open connection, execute query, and close connection.
	cn->Open();
	if (cmd->ExecuteNonQuery() == 1)
	cn->Close();

	Console::WriteLine("File2OleDbBlob completed successfully.\nPress return to continue.");
	Console::ReadLine();
	}catch(OleDbException *ex)
	 {Console::Write(ex->Message);}
   }
				

?????? ??????

  • ?? ???? ??? ????? ???? ??? ??? ?????? ?? ???? LongVarChar ?? LongVarWChar ??????? ?? ??? ????? ???? ?? ??? ??????? ???? ?? ???? ???
  • ?? ???? ????? ?? ??? ??????? ???????? ?? SQL ??? ??????? ???? ????? ???? ??, ???? ????? ?????? ?????? ???? ?? ?? ??? ?? ??? ???? ?????? ?? ??? ?????? ???? ???? ???

??????

???????? ??????? ?? ???, ???? ?? ???? ?????? ?? ????? ?? ???? ?? Microsoft ???????? ??? ?????::
317044TO HOW: ????? ?? ????? ???? ????? ?? ??? ?? ?? ????? ????? ?? ADO.NET ?? C++ ??? Chunking ?? ????? ????
????? C++ .NET ??? .NET ???????? ??????? ?? ????? ???? ?? ???? ??? ???????? ??????? ?? ??? Microsoft ???????? ??? ???? ????? ?? ??? ????? ???? ?????? ?? ????? ????:
313480???????: .NET ???? ??????? ?? ??? Roadmap
BLOBs ADO.NET ??? ??? ???? ?? ???? ??? ???? ??????? ?? ??? ????? Microsoft ??? ???? ?? ????:
???? ??????? ?? ????? ??? ??????? ?? ??? ??
HTTP://MSDN.Microsoft.com/Library/default.asp?URL=/Library/en-us/cpguide/HTML/cpconobtainingblobvaluesfromdatabase.asp

???

???? ID: 317017 - ????? ???????: 24 ????? 2014 - ??????: 2.0
???? ???? ???? ??:
  • Microsoft ADO.NET 1.1
  • Microsoft Visual C++ .NET 2002 Standard Edition
  • Microsoft Visual C++ .NET 2003 Standard Edition
??????: 
kbnosurvey kbarchive kbhowtomaster kbsqlclient kbsystemdata kbmt KB317017 KbMthi
???? ?????? ????????
??????????: ?? ???? ?? ???? ??????? ?? ????? ?? Microsoft ????-?????? ?????????? ?????? ?????? ???? ??? ??. Microsoft ???? ??? ????-???????? ?? ????-???????? ????? ?????? ?? ???? ???????? ???? ?? ???? ????? ????? ??? ?? ??? ?????? ?? ???? ???? ???? ??? ????? ??. ???????, ????-???????? ???? ????? ???? ???? ???? ???. ?????, ????????, ?????-???? ?? ??????? ?? ???????? ?? ???? ???, ???? ?? ??? ?????? ???? ???? ??? ????? ??? ?? ???? ??. Microsoft ??????? ??? ???? ?? ?????? ?? ??????????, ????????? ?? ??? ?????? ?? ???? ????? ?? ???? ???????? ?? ??? ???? ????? ?? ??? ????????? ???? ??. Microsoft ????-?????? ?????????? ?? ????? ?????? ?? ?? ??? ??.
?????????? ?? ??????? ????????? ??????? ??:317017

??????????? ???

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com