直接執行: SQLRETURN SQLExecDirect(執行的語句句柄,SQL語句,SQL語句緩沖區大小); SQLChar SQLStatement ="SELECT * FROM TABLE"; SQLExecDirect(hstmt,&SQLStatement,SQL_NTS);
這是MSDN上的一個例子:include <windows.h> #include <stdio.h> #include <sql.h> #include <sqlext.h>int main(int argc, char* argv[]) { SQLCHAR* theDiagState = new SQLCHAR[50]; SQLINTEGER theNativeState; SQLCHAR* theMessageText = new SQLCHAR[255]; SQLSMALLINT iOutputNo; SQLHENV m_SQLEnvironment; SQLHDBC m_SQLConnection; SQLHSTMT m_SQLStatement; SQLRETURN iReturn; DWORD returnValue = 0; DWORD returnValue1 = 0; long lBufLength = sizeof(returnValue); long lBufLength1 = sizeof(returnValue1); SQLCHAR theNumeric2[50]; SQLINTEGER cbNumeric, cbNumeric2; strcpy((char*)theNumeric2, "5.9"); //Connect //Allocate Environment Handle iReturn = SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&m_SQLEnvironment); //Set environment to ODBC_3 iReturn = SQLSetEnvAttr(m_SQLEnvironment,SQL_ATTR_ODBC_VERSION,(SQLPOINTER) SQL_OV_ODBC3,0); //Allocate connection handle iReturn = SQLAllocHandle(SQL_HANDLE_DBC,m_SQLEnvironment,&m_SQLConnection); //Connect to the database. //In this example we have used the following //Pubs as the DSN name //sa is the login name without a password //CHANGE THE DSN NAME HERE along with the length of the DSN.
iReturn = SQLConnect(m_SQLConnection,(SQLCHAR*) "Pubs",4,(SQLCHAR*)"sa",2,(SQLCHAR*)"",0); if (iReturn != SQL_ERROR) {
//Run Query //Allocate the statement handle iReturn = SQLAllocHandle(SQL_HANDLE_STMT,m_SQLConnection,&m_SQLStatement); //If you want to insert NULL data, the last parameter of SQLBindParameter should contain SQL_NULL_DATA cbNumeric = SQL_NULL_DATA; cbNumeric2 = SQL_NTS; //Sending Null Data iReturn = SQLBindParameter(m_SQLStatement,1,SQL_PARAM_INPUT,SQL_C_NUMERIC,SQL_NUMERIC,13,6,NULL,0,&cbNumeric); if (iReturn != SQL_SUCCESS) { SQLGetDiagRec(SQL_HANDLE_STMT,m_SQLStatement,1,theDiagState,&theNativeState,theMessageText,100,&iOutputNo); }
//Sending nonNull Data iReturn = SQLBindParameter(m_SQLStatement,2,SQL_PARAM_INPUT,SQL_C_CHAR,SQL_NUMERIC,13,6,theNumeric2,0,&cbNumeric2); if (iReturn != SQL_SUCCESS) { SQLGetDiagRec(SQL_HANDLE_STMT,m_SQLStatement,1,theDiagState,&theNativeState,theMessageText,100,&iOutputNo); } //Execute the statement to insert a null and a non-null value to the table. //CHANGE THE TABLE/COLUMN NAME HERE. //In this case we have used a table called TestNULL in SQL Server 7.0 with two fields //theNumber1 : Numeric Field that allows NULL data (Length =13, Precision = 28, Scale=6) //theNumber2 : Numeric that does not allow NULL data (Length =13, Precision = 28, Scale=6) iReturn = SQLExecDirect(m_SQLStatement,(SQLCHAR*) "Insert Into TestNull (theNumber1,theNumber2) Values (?,?)",SQL_NTS); if (iReturn != SQL_SUCCESS) { SQLGetDiagRec(SQL_HANDLE_STMT,m_SQLStatement,1,theDiagState,&theNativeState,theMessageText,100,&iOutputNo); } //DISCONNECT iReturn = SQLFreeHandle(SQL_HANDLE_STMT,m_SQLStatement); iReturn = SQLDisconnect(m_SQLConnection); iReturn = SQLFreeHandle(SQL_HANDLE_DBC,m_SQLConnection); iReturn = SQLFreeHandle(SQL_HANDLE_ENV,m_SQLEnvironment); m_SQLStatement = NULL; m_SQLConnection = NULL; m_SQLEnvironment = NULL; } else { //If it fails to connect theMessageText contains the reason for failure SQLGetDiagRec(SQL_HANDLE_DBC,m_SQLConnection,1,theDiagState,&theNativeState,theMessageText,100,&iOutputNo); } delete theMessageText; delete theDiagState; return 1; }
if(0 == imagefile.Open(文件名,CFile::modeRead))
return;
_RecordsetPtr pRs = NULL;
_ConnectionPtr pConnection = NULL;
_variant_t varChunk;
HRESULT hr;
BYTE* pbuf;
long nLength = imagefile.GetLength();
pbuf = new BYTE[nLength+2];
if(pbuf == NULL)
return; //allocate memory error;
imagefile.Read(pbuf,nLength); //read the file into memory BYTE *pBufEx;
pBufEx = pbuf;
//build a SAFFERRAY
SAFEARRAY* psa;
SAFEARRAYBOUND rgsabound[1];
rgsabound[0].lLbound = 0;
rgsabound[0].cElements = nLength;
psa = SafeArrayCreate(VT_UI1, 1, rgsabound); for (long i = 0; i < nLength; i++)
SafeArrayPutElement (psa, &i, pBufEx++);
VARIANT varBLOB;
varBLOB.vt = VT_ARRAY | VT_UI1;
varBLOB.parray = psa; _bstr_t strCnn("Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=CUSTOM;Data Source=SERVER");
try
{
//Open a connection
pConnection.CreateInstance(__uuidof(Connection));
hr = pConnection->Open(strCnn,"","",NULL); //Connect a DataBase
pRs.CreateInstance(__uuidof(Recordset));
pRs->Open("CustomInfo",_variant_t((IDispatch *) pConnection,true),adOpenKeyset,adLockOptimistic,adCmdTable); //Open a Table
// pRs->AddNew();
pRs->Fields->GetItem("Image")->AppendChunk(varBLOB);
pRs->Update();
pRs->Close();
pConnection->Close();
}
catch(_com_error &e)
{
// Notify the user of errors if any.
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
CString sError;
sError.Format("Source : %s \n Description : %s\n",(LPCSTR)bstrSource,(LPCSTR)bstrDescription);
AfxMessageBox(sError);
}
_bstr_t strCnn("Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=CUSTOM;Data Source=SERVER");
try
{
//Open a connection
pConnection.CreateInstance(__uuidof(Connection));
hr = pConnection-> Open(strCnn,"","",NULL); //Connect a DataBase
pRs.CreateInstance(__uuidof(Recordset));
pRs-> Open("CustomInfo",_variant_t((IDispatch *) pConnection,true),adOpenKeyset,adLockOptimistic,adCmdTable); //Open a Table
// pRs-> AddNew();
pRs-> Fields-> GetItem("Image")-> AppendChunk(varBLOB);
pRs-> Update();
pRs-> Close();
pConnection-> Close();
}
catch(_com_error & e)
{
// Notify the user of errors if any.
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
CString sError;
sError.Format("Source : %s \n Description : %s\n",(LPCSTR)bstrSource,(LPCSTR)bstrDescription);
AfxMessageBox(sError);
改成ODBC怎么改?
_bstr_t strCnn("Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=CUSTOM;Data Source=SERVER");
try
{
//Open a connection
pConnection.CreateInstance(__uuidof(Connection));
hr = pConnection-> Open(strCnn,"","",NULL); //Connect a DataBase
pRs.CreateInstance(__uuidof(Recordset));
pRs-> Open("CustomInfo",_variant_t((IDispatch *) pConnection,true),adOpenKeyset,adLockOptimistic,adCmdTable); //Open a Table
// pRs-> AddNew();
pRs-> Fields-> GetItem("Image")-> AppendChunk(varBLOB);
pRs-> Update();
pRs-> Close();
pConnection-> Close();
}
catch(_com_error & e)
{
// Notify the user of errors if any.
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
CString sError;
sError.Format("Source : %s \n Description : %s\n",(LPCSTR)bstrSource,(LPCSTR)bstrDescription);
AfxMessageBox(sError);
改成ODBC怎么改?
SQLHDBC hdbc;
SQLHSTMT hstmt;
SQLRETURN rtcode;//分配環境句柄
rtcode = SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&henv);
if(rtcode == SQL_SUCCESS || rtcode == SQL_SUCCESS_WITH_INFO)
{
//設置環境屬性(ODBC版本號)
rtcode = SQLSetEnvAttr(henv,SQL_ATTR_ODBC_VERSION,(void*)SQL_OV_ODBC3,0);
if(rtcode == SQL_SUCCESS || rtcode == SQL_SUCCESS_WITH_INFO)
{
//分配連接句柄
rtcode = SQLAllocHandle( SQL_HANDLE_DBC, henv, &hdbc);
if(rtcode == SQL_SUCCESS || rtcode == SQL_SUCCESS_WITH_INFO)
{ //設置連接屬性(超時時間10s)
SQLSetConnectAttr(hdbc,SQL_ATTR_LOGIN_TIMEOUT,(void*)10,0); //連接數據源
LPTSTR lpstrDsn="Provider=SQLOLEDB;SERVER=服務器名;DATABASE=數據庫名;UID=用戶;PWD=口令;"
rtcode = SQLConnect(hdbc,(SQLCHAR*)lpstrDsn,SQL_NTS,(SQLCHAR*)"",SQL_NTS,(SQLCHAR*)"",SQL_NTS);
if(rtcode == SQL_SUCCESS || rtcode == SQL_SUCCESS_WITH_INFO)
{ //分配語句句柄
rtcode = SQLAllocHandle(SQL_HANDLE_STMT,hdbc,&hstmt);
if(rtcode == SQL_SUCCESS || rtcode == SQL_SUCCESS_WITH_INFO)
{
*********執行操作**************** //釋放語句句柄
SQLFreeHandle(SQL_HANDLE_STMT,hstmt);
//斷開連接
SQLDisconnect(hdbc);
//釋放連接句柄
SQLFreeHandle(SQL_HANDLE_DBC,hdbc);
//釋放環境句柄
SQLFreeHandle(SQL_HANDLE_ENV,henv);
SQLRETURN SQLExecDirect(執行的語句句柄,SQL語句,SQL語句緩沖區大小);
SQLChar SQLStatement ="SELECT * FROM TABLE";
SQLExecDirect(hstmt,&SQLStatement,SQL_NTS);
#include <stdio.h>
#include <sql.h>
#include <sqlext.h>int main(int argc, char* argv[])
{
SQLCHAR* theDiagState = new SQLCHAR[50]; SQLINTEGER theNativeState;
SQLCHAR* theMessageText = new SQLCHAR[255]; SQLSMALLINT iOutputNo; SQLHENV m_SQLEnvironment;
SQLHDBC m_SQLConnection;
SQLHSTMT m_SQLStatement; SQLRETURN iReturn;
DWORD returnValue = 0;
DWORD returnValue1 = 0;
long lBufLength = sizeof(returnValue);
long lBufLength1 = sizeof(returnValue1);
SQLCHAR theNumeric2[50];
SQLINTEGER cbNumeric, cbNumeric2;
strcpy((char*)theNumeric2, "5.9"); //Connect
//Allocate Environment Handle
iReturn = SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&m_SQLEnvironment); //Set environment to ODBC_3
iReturn = SQLSetEnvAttr(m_SQLEnvironment,SQL_ATTR_ODBC_VERSION,(SQLPOINTER) SQL_OV_ODBC3,0); //Allocate connection handle
iReturn = SQLAllocHandle(SQL_HANDLE_DBC,m_SQLEnvironment,&m_SQLConnection); //Connect to the database.
//In this example we have used the following
//Pubs as the DSN name
//sa is the login name without a password
//CHANGE THE DSN NAME HERE along with the length of the DSN.
iReturn = SQLConnect(m_SQLConnection,(SQLCHAR*) "Pubs",4,(SQLCHAR*)"sa",2,(SQLCHAR*)"",0);
if (iReturn != SQL_ERROR)
{
//Run Query
//Allocate the statement handle
iReturn = SQLAllocHandle(SQL_HANDLE_STMT,m_SQLConnection,&m_SQLStatement);
//If you want to insert NULL data, the last parameter of SQLBindParameter should contain SQL_NULL_DATA
cbNumeric = SQL_NULL_DATA;
cbNumeric2 = SQL_NTS;
//Sending Null Data
iReturn = SQLBindParameter(m_SQLStatement,1,SQL_PARAM_INPUT,SQL_C_NUMERIC,SQL_NUMERIC,13,6,NULL,0,&cbNumeric);
if (iReturn != SQL_SUCCESS)
{
SQLGetDiagRec(SQL_HANDLE_STMT,m_SQLStatement,1,theDiagState,&theNativeState,theMessageText,100,&iOutputNo);
}
//Sending nonNull Data
iReturn = SQLBindParameter(m_SQLStatement,2,SQL_PARAM_INPUT,SQL_C_CHAR,SQL_NUMERIC,13,6,theNumeric2,0,&cbNumeric2);
if (iReturn != SQL_SUCCESS)
{
SQLGetDiagRec(SQL_HANDLE_STMT,m_SQLStatement,1,theDiagState,&theNativeState,theMessageText,100,&iOutputNo);
} //Execute the statement to insert a null and a non-null value to the table.
//CHANGE THE TABLE/COLUMN NAME HERE.
//In this case we have used a table called TestNULL in SQL Server 7.0 with two fields
//theNumber1 : Numeric Field that allows NULL data (Length =13, Precision = 28, Scale=6)
//theNumber2 : Numeric that does not allow NULL data (Length =13, Precision = 28, Scale=6)
iReturn = SQLExecDirect(m_SQLStatement,(SQLCHAR*) "Insert Into TestNull (theNumber1,theNumber2) Values (?,?)",SQL_NTS);
if (iReturn != SQL_SUCCESS)
{
SQLGetDiagRec(SQL_HANDLE_STMT,m_SQLStatement,1,theDiagState,&theNativeState,theMessageText,100,&iOutputNo);
} //DISCONNECT
iReturn = SQLFreeHandle(SQL_HANDLE_STMT,m_SQLStatement);
iReturn = SQLDisconnect(m_SQLConnection);
iReturn = SQLFreeHandle(SQL_HANDLE_DBC,m_SQLConnection);
iReturn = SQLFreeHandle(SQL_HANDLE_ENV,m_SQLEnvironment); m_SQLStatement = NULL;
m_SQLConnection = NULL;
m_SQLEnvironment = NULL;
}
else
{
//If it fails to connect theMessageText contains the reason for failure
SQLGetDiagRec(SQL_HANDLE_DBC,m_SQLConnection,1,theDiagState,&theNativeState,theMessageText,100,&iOutputNo); } delete theMessageText;
delete theDiagState;
return 1;
}