用Vc访问SQL Server库除了ODBC还有其它方法吗?

sintony 2000-02-15 11:11:00
我不想去控制面版添加System DSN
...全文
294 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
first_david 2000-02-17
  • 打赏
  • 举报
回复
DB-Library 不错,参Msdn\...\..SQL SERVER..\...DB-Library for c..
netmare 2000-02-16
  • 打赏
  • 举报
回复
OleDB or ADO also based on ODBC
DOU 2000-02-16
  • 打赏
  • 举报
回复
完整的参考
How to add a data source (ODBC)
You can add a data source by using ODBC Administrator, programmatically (by using SQLConfigDataSource), or by creating a file.

To add a data source by using ODBC Administrator

On the Start menu, point to Settings, and then click Control Panel.
Double-click ODBC.
Click the User DSN, System DSN, or File DSN tab, and then click Add.
Click SQL Server; then click Finish.
Complete the steps in the Create a New Data Source to SQL Server Wizard.
To add a data source programmatically

Call SQLConfigDataSource with the fOption set to either ODBC_ADD_DSN or ODBC_ADD_SYS_DSN.
To add a file data source

Call SQLDriverConnect with a SAVEFILE=file_name parameter in the connect string. If the connect is successful, the ODBC driver creates a file data source with the connection parameters in the location pointed to by the SAVEFILE parameter.
Examples
A. Create a data source using SQLConfigDataSource
#include <stdio.h>

#include <windows.h>

#include "sql.h"

#include <odbcinst.h>



int main()

{

RETCODE retcode;



UCHAR *szDriver = "SQL Server";

UCHAR *szAttributes =

"DSN=MyDSN\0DESCRIPTION=SQLConfigDSN Sample\0"

"SERVER=MySQL\0ADDRESS=MyServer\0NETWORK=dbmssocn\0"

"DATABASE=pubs\0";



retcode = SQLConfigDataSource(NULL,

ODBC_ADD_DSN,

szDriver,

szAttributes);



B. Create a file data source
Use the SAVEFILE keyword in SQLDriverConnect to create a file data source, and then use SQLDriverConnect to connect with the file data source. This example has been simplified by removing error handling.

#include <stdio.h>

#include <string.h>

#include <windows.h>

#include <sql.h>

#include <sqlext.h>

#include <odbcss.h>



#define MAXBUFLEN 255



SQLHENV henv = SQL_NULL_HENV;

SQLHDBC hdbc1 = SQL_NULL_HDBC;



int main() {



RETCODE retcode;



// This format of the SAVEFILE keyword saves a successful

// connection as the file Myfiledsn.dsn in the ODBC default

// directory for file DSNs.

SQLCHAR szConnStrIn[MAXBUFLEN] =

"SAVEFILE=MyFileDSN;DRIVER={SQL Server};SERVER=MySQL;"

"NETWORK=dbmssocn;UID=sa;PWD=MyPassWord;";



SQLCHAR szConnStrOut[MAXBUFLEN];

SQLSMALLINT cbConnStrOut = 0;



// Allocate the ODBC Environment and save handle.

retcode = SQLAllocHandle (SQL_HANDLE_ENV, NULL, &henv);



// Let ODBC know this is an ODBC 3.0 application.

retcode = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION,

(SQLPOINTER) SQL_OV_ODBC3, SQL_IS_INTEGER);



// Allocate an ODBC connection handle and connect.

retcode = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc1);

retcode = SQLDriverConnect(hdbc1, // Connection handle

NULL, // Window handle

szConnStrIn, // Input connect string

SQL_NTS, // Null-terminated string

szConnStrOut, // Addr of output buffer

MAXBUFLEN, // Size of output buffer

&cbConnStrOut, // Address of output length

SQL_DRIVER_NOPROMPT);



// Disconnect, set up a new connect string, and then test file DSN.

SQLDisconnect(hdbc1);

strcpy(szConnStrIn, "FILEDSN=MyFileDSN;UID=sa;PWD=MyPassWord;");

retcode = SQLDriverConnect(hdbc1, // Connection handle

NULL, // Window handle

szConnStrIn, // Input connect string

SQL_NTS, // Null-terminated string

szConnStrOut, // Addr of output buffer

MAXBUFLEN, // Size of output buffer

&cbConnStrOut, // Address of output length

SQL_DRIVER_NOPROMPT);



/* Clean up. */

SQLDisconnect(hdbc1);

SQLFreeHandle(SQL_HANDLE_DBC, hdbc1);

SQLFreeHandle(SQL_HANDLE_ENV, henv);

return(0);

}


DOU 2000-02-16
  • 打赏
  • 举报
回复
其实使用ODBC也可以不用手工在控制面版添加System DSN
可以使用SQLConfigDataSource 函数编程添加DSN,只要将SQLConfigDataSource
的 fOption 参数置为ODBC_ADD_DSN 或 ODBC_ADD_SYS_DSN 即可
jco 2000-02-16
  • 打赏
  • 举报
回复
使用ADO,连接connect.open("Provider=MSDASQL;DRIVER={SQL SERVER};
SERVER="你的server名";UID=sa;pwd=;")
cloud 2000-02-16
  • 打赏
  • 举报
回复
#import "c:\Program Files\Common Files\System\ADO\msado15.dll" \
no_namespace rename("EOF", "EndOfFile")
#include <stdio.h>

// Note 1
inline void TESTHR( HRESULT _hr )
{ if FAILED(_hr) _com_issue_error(_hr); }

void main(void)
{
CoInitialize(NULL);
try
{
_RecordsetPtr pRs("ADODB.Recordset");
_ConnectionPtr pCn("ADODB.Connection");
_variant_t vtTableName("authors"),
vtCriteria;
long ix[1];
SAFEARRAY *pSa = NULL;

pCn->Open("DSN=pubs;User ID=sa;pwd=;Provider=MSDASQL;", "", "",
adConnectUnspecified);
// Note 2, Note 3
pSa = SafeArrayCreateVector(VT_VARIANT, 1, 4);
if (!pSa) _com_issue_error(E_OUTOFMEMORY);

// Specify TABLE_NAME in the third array element (index of 2).

ix[0] = 2;
TESTHR(SafeArrayPutElement(pSa, ix, &vtTableName));

// There is no Variant constructor for a SafeArray, so manually set the
// type (SafeArray of Variant) and value (pointer to a SafeArray).

vtCriteria.vt = VT_ARRAY and VT_VARIANT;
vtCriteria.parray = pSa;

pRs = pCn->OpenSchema(adSchemaColumns, vtCriteria, vtMissing);

long limit = pRs->GetFields()->Count;
for (long x = 0; x < limit; x++)
printf("%d: %s\n", x+1,
((char*) pRs->GetFields()->Item[x]->Name));
// Note 4
pRs->Close();
pCn->Close();
}
catch (_com_error &e)
{
printf("Error:\n");
printf("Code = %08lx\n", e.Error());
printf("Code meaning = %s\n", e.ErrorMessage());
printf("Source = %s\n", (char*) e.Source());
printf("Description = %s\n", (char*) e.Description());
}
::CoUninitialize();
}

在MSDN中用ADO可查到。
rober 2000-02-16
  • 打赏
  • 举报
回复
Ado,Dao都可以用啊,我有一个ADO的经典范例,想要的话发邮件给我。
cloud 2000-02-16
  • 打赏
  • 举报
回复
用ADO,这么先进的东东你怎么忘了。
tonyhan 2000-02-16
  • 打赏
  • 举报
回复
好像还有OLDB这样的东东。
Eros 2000-02-16
  • 打赏
  • 举报
回复
最新版本的VC中有DB ENGINEERING提供了对SQL server的极其完善的支持。
magic 2000-02-16
  • 打赏
  • 举报
回复
使用ADO or oldb
wuxfBrave 2000-02-16
  • 打赏
  • 举报
回复
ADO or OlEDB
sintony 2000-02-15
  • 打赏
  • 举报
回复
具体一点好吗?
cool 2000-02-15
  • 打赏
  • 举报
回复
以上

16,466

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧