各位XDJM:我在仿照MSDN上的实例(参看:http://support.microsoft.com/kb/317017)想做一个能读写数据库图片的程序,但是编译时提示:fatal error C1021: invalid preprocessor command 'using',指向的是这一行:“#using <mscorlib.dll>” 在网上找了下都没有该问题好的解释,各位能给下原因吗? 或者谁能帮忙给份能够编译通过的实例参考下,本人刚学VC,多多包涵,谢谢! #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:\\pic\\Kevin Garnett.jpg"; // 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;
}
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);}
}
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);}
}
#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:\\pic\\Kevin Garnett.jpg"; // 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;
}
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);}
}
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);}
}
解决方案 »
- SeaShell编译不通过?
- VC6编译链接时出现unresolved external symbol错误
- UDP 在局域网中传输文件时严重丢包,如何解决?
- 保存为WAV文件
- 谁有封装好的SMTP类?最好有附件处理
- 假如一个DLL自己动态载入了第2个DLL 然后这个DLL提前被FreeLibrary,那么如何FreeLibrary第2个DLL呢?
- 如何判断一个CBrush对象是否被CreateSolidBrush???
- 请教:
- 有没有像表格一样的控件,只有2列,左边是文字说明,右边是如编辑框,组合框等的输入
- Activex dll
- wchar读取(fread)文件乱码问题!
- 自定义工具栏初始停靠位置问题
原文:在 Visual N 创建一个新的托管 C++ 应用程序。
在 Visual 的.NET 2003 中创建新控制台应用程序 (.NET)。