SQL链接数据库

Standard   Security:  
   
  "Data   Source=Aron1;Initial   Catalog=pubs;User   Id=sa;Password=asdasd;"    
        -   or   -  
  "Server=Aron1;Database=pubs;User   ID=sa;Password=asdasd;Trusted_Connection=False"    
        (both   connection   strings   produces   the   same   result)  
   
   
   
   
    Trusted   Connection:  
   
  "Data   Source=Aron1;Initial   Catalog=pubs;Integrated   Security=SSPI;"    
        -   or   -  
  "Server=Aron1;Database=pubs;Trusted_Connection=True;"    
        (both   connection   strings   produces   the   same   result)  
   
  (use   serverName/instanceName   as   Data   Source   to   use   an   specifik   SQLServer   instance)  
    Connect   via   an   IP   address:  
  "Data   Source=190.190.200.100,1433;Network   Library=DBMSSOCN;Initial   Catalog=pubs;User   ID=sa;Password=asdasd;"    
  (DBMSSOCN=TCP/IP   instead   of   Named   Pipes,   at   the   end   of   the   Data   Source   is   the   port   to   use   (1433   is   the   default))  
    Enabling   MARS   (multiple   active   result   sets):  
   
  "Server=Aron1;Database=pubs;Trusted_Connection=True;MultipleActiveResultSets=true"    
  Note!   Use   ADO.NET   2.0   for   MARS   functionality.   MARS   is   not   supported   in   ADO.NET   1.0   nor   ADO.NET   1.1  
   
  Streamline   your   Data   Connections   by   Moving   to   MARS,   by   Laurence   Moroney,   DevX.com   >>  
    Attach   a   database   file   on   connect   to   a   local   SQL   Server   Express   instance:  
   
  "Server=./SQLExpress;AttachDbFilename=c:/asd/qwe/mydbfile.mdf;Database=dbname;Trusted_Connection=Yes;"    
        -   or   -  
  "Server=./SQLExpress;AttachDbFilename=|DataDirectory|mydbfile.mdf;Database=dbname;Trusted_Connection=Yes;"    
        (use   |DataDirectory|   when   your   database   file   resides   in   the   data   directory)    
  Why   is   the   "Database"   parameter   needed?   Answer:   If   the   database   was   previously   attached,   SQL   Server   does   not   reattach   it   (it   uses   the   attached   database   as   the   default   for   the   connection).  
    Using   "User   Instance"   on   a   local   SQL   Server   Express   instance:  
   
  "Data   Source=./SQLExpress;integrated   security=true;attachdbfilename=|DataDirectory|/mydb.mdf;user   instance=true;"    
  The   "User   Instance"   functionality   creates   a   new   SQL   Server   instance   on   the   fly   during   connect.   This   works   only   on   a   local   SQL   Server   2005   instance   and   only   when   connecting   using   windows   authentication   over   local   named   pipes.   The   purpose   is   to   be   able   to   create   a   full   rights   SQL   Server   instance   to   a   user   with   limited   administrative   rights   on   the   computer.   To   enable   the   functionality:   sp_configure   'user   instances   enabled','1'   (0   to   disable)  
  Using   SQL   Server   2005   Express?   Don't   miss   the   server   name   syntax:   SERVERNAME/SQLEXPRESS   (Substitute   "SERVERNAME"   with   the   name   of   the   computer)

vc链接SQL Server数据库的步骤如下: 1. 首先,在VC项目中添加与SQL Server数据库连接相关的头文件和库文件。头文件主要包括"stdafx.h"和"sql.h",库文件主要包括"odbc32.lib"和"odbccp32.lib"。 2. 在VC项目中创建一个数据库连接对象,可以使用ODBC(Open Database Connectivity)来进行操作。首先定义一个数据库连接句柄,例如"HDBC hconn",并使用SQLAllocHandle函数创建一个数据库连接。示例代码如下: HDBC hconn; SQLAllocHandle(SQL_HANDLE_DBC, SQL_NULL_HANDLE, &hconn); 3. 设置连接属性。可以使用SQLSetConnectAttr函数为连接设置不同的属性,例如设置连接超时时长,示例代码如下: SQLSetConnectAttr(hconn, SQL_LOGIN_TIMEOUT, (SQLPOINTER)5, SQL_IS_INTEGER); 4. 使用SQLConnect函数连接到SQL Server数据库。为SQLConnect函数提供正确的连接参数,包括服务器名、用户名、密码等信息。示例代码如下: SQLConnect(hconn, L"server_name", SQL_NTS, L"user_name", SQL_NTS, L"password", SQL_NTS); 5. 连接成功后,可以使用SQLExecDirect函数执行SQL语句。通过组合SQL语句来进行数据库的增删改查操作。示例代码如下: SQLExecDirect(hconn, L"SELECT * FROM table_name", SQL_NTS); 6. 执行完毕后,使用SQLDisconnect函数断开与SQL Server数据库的连接。示例代码如下: SQLDisconnect(hconn); 以上步骤是通过ODBC连接SQL Server数据库的基本流程,通过编写相应的代码并进行调试,就可以实现VC与SQL Server数据库的连接和操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值