将C ++ win32控制台应用程序连接到sql数据库? [英] Connecting C++ win32 console application to sql database?

查看:216
本文介绍了将C ++ win32控制台应用程序连接到sql数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将C ++ win32控制台应用程序连接到SQL数据库。我已将C#表单应用程序连接到数据库,但不知道将VC ++中的控制台应用程序连接到SQL数据库。我做谷歌但没有找到满意的答案,任何人都可以帮助代码和查询吗?

解决方案

一些建议此处 [ ^ ]和特定的Microsoft信息此处 [ ^ ]。


例如访问SQ L服务器:

  #define DBNTWIN32 
#include < stdio.h < span class =code-keyword>>
#include < span class =code-keyword>< windows.h >
#include < sqlfront.h >
# include < sqldb.h >

// 错误处理程序和消息处理程序的前向声明。

int err_handler(PDBPROCESS,INT,INT,INT,LPCSTR,LPCSTR);
int msg_handler(PDBPROCESS,DBINT,INT,INT,LPCSTR,LPCSTR,
LPCSTR,DBUSMALLINT);
main()
{
PDBPROCESS dbproc; // 与SQL Server的连接。
PLOGINREC登录; // 登录信息。
DBCHAR名称[ 100 ];
DBCHAR city [ 100 ];

// 安装用户提供的错误和消息处理功能。

dberrhandle(err_handler);
dbmsghandle(msg_handler);

// 初始化DB-Library。

dbinit();

// 获取LOGINREC。

login = dblogin();
DBSETLUSER(登录, my_login);
DBSETLPWD(登录, my_password);
DBSETLAPP(登录, example);

// 获取DBPROCESS结构以与SQL Server进行通信。

dbproc = dbopen(登录, my_server) ;

// $ b $中的authors表中检索一些列b // pubs database。
// 首先,将命令放入命令缓冲区。

dbcmd(dbproc, SELECT au_lname,city FROM pubs..authors );
dbcmd(dbproc, WHERE state ='CA');

// 将命令发送到SQL Server并开始执行。

dbsqlexec(dbproc);

// 处理结果。

if (dbresults(dbproc)== SUCCEED)
{

< span class =code-comment> //
将列绑定到程序变量。

dbbind(dbproc, 1 ,NTBSTRINGBIND, 0 ,名称);
dbbind(dbproc, 2 ,NTBSTRINGBIND, 0 ,city);

// 检索并打印结果行。

while (dbnextrow(dbproc)!= NO_MORE_ROWS)
{
printf( %s来自%s \ n,name,city);
}
}

// 关闭与SQL Server的连接。

dbexit();
return 0 );
}

int err_handler(PDBPROCESS dbproc,INT severity,
INT dberr,INT oserr,LPCSTR dberrstr,LPCSTR oserrstr)
{
printf( DB-Library Error%i:%s \ n ,dberr,dberrstr);
if (oserr!= DBNOERR)
{
printf( 操作系统错误%i:%s \ n,oserr,oserrstr);
}
return (INT_CANCEL);
}

int msg_handler(PDBPROCESS dbproc,DBINT msgno,INT msgstate,
INT severity,LPCSTR msgtext,LPCSTR服务器,
LPCSTR过程,DBUSMALLINT行)
{
printf( SQL Server消息%ld:%s \ n,msgno,msgtext);
return 0 );
}





PS

您可以使用数据源开放式数据库连接(ODBC)来访问来自各种数据库管理系统的数据。例如,如果您有一个访问SQL数据库中的数据的程序,数据源(ODBC)将允许您使用相同的程序访问Visual FoxPro数据库中的数据。为此,您必须将称为驱动程序的软件组件添加到系统中。数据源(ODBC)可帮助您添加和配置这些驱动程序。开始 - >设置 - >控制面板 - >管理工具 - >数据源(ODBC)请看这里:

http://msdn.microsoft.com/en-us/library/2x0tte0f.aspx


ref link http://stackoverflow.com/questions/11530080/c-odbc-sql-server-2008-connection [ ^

I want to connect a C++ win32 console application to a SQL database. I have connected a C# form application to a database but don't have any idea about connecting console application in VC++ to a SQL database. I did Google but didn't find a satisfactory answer, can anybody help with the code and query?

解决方案

Some suggestions here[^], and specific Microsoft information here[^].


For example the accessing SQL Server :

#define DBNTWIN32
#include <stdio.h>
#include <windows.h>
#include <sqlfront.h>
#include <sqldb.h>
 
// Forward declarations of the error handler and message handler. 
 
int err_handler(PDBPROCESS, INT, INT, INT, LPCSTR, LPCSTR);
int msg_handler(PDBPROCESS, DBINT, INT, INT, LPCSTR, LPCSTR,
LPCSTR, DBUSMALLINT);
main()
{
   PDBPROCESS dbproc; // The connection with SQL Server. 
   PLOGINREC login; // The login information. 
   DBCHAR name[100];
   DBCHAR city[100];
 
// Install user-supplied error- and message-handling functions.
 
   dberrhandle (err_handler);
   dbmsghandle (msg_handler);
 
// Initialize DB-Library.
 
   dbinit ();
 
// Get a LOGINREC.
 
   login = dblogin ();
   DBSETLUSER (login, "my_login");
   DBSETLPWD (login, "my_password");
   DBSETLAPP (login, "example");
 
// Get a DBPROCESS structure for communication with SQL Server. 
 
   dbproc = dbopen (login, "my_server");
 
// Retrieve some columns from the authors table in the
// pubs database.
// First, put the command into the command buffer. 
 
   dbcmd (dbproc, "SELECT au_lname, city FROM pubs..authors");
   dbcmd (dbproc, " WHERE state = 'CA' ");
 
// Send the command to SQL Server and start execution. 
 
   dbsqlexec (dbproc);
 
// Process the results. 
 
   if (dbresults (dbproc) == SUCCEED)
   {
 
// Bind column to program variables. 
 
      dbbind (dbproc, 1, NTBSTRINGBIND, 0, name);
      dbbind (dbproc, 2, NTBSTRINGBIND, 0, city);
 
// Retrieve and print the result rows. 
 
      while (dbnextrow (dbproc) != NO_MORE_ROWS)
      {
         printf ("%s from %s\n", name, city);
      }
   }
 
// Close the connection to SQL Server. 
 
   dbexit ();
   return (0);
}
 
int err_handler (PDBPROCESS dbproc, INT severity,
INT dberr, INT oserr, LPCSTR dberrstr, LPCSTR oserrstr)
{
   printf ("DB-Library Error %i: %s\n", dberr, dberrstr);
   if (oserr != DBNOERR)
   {
      printf ("Operating System Error %i: %s\n", oserr, oserrstr);
   }
   return (INT_CANCEL);
}
 
int msg_handler (PDBPROCESS dbproc, DBINT msgno, INT msgstate,
INT severity, LPCSTR msgtext, LPCSTR server,
LPCSTR procedure, DBUSMALLINT line)
{
   printf ("SQL Server Message %ld: %s\n", msgno, msgtext);
   return (0);
}



P.S.
You can use Data Sources Open Database Connectivity (ODBC) to access data from a variety of database management systems. For example, if you have a program that accesses data in a SQL database, Data Sources (ODBC) will let you use the same program to access data in a Visual FoxPro database. To do this, you must add software components called drivers to your system. Data Sources (ODBC) helps you add and configure these drivers. start->Settings->Control Panel->Administrative Tools->Data Sources (ODBC) Please see here:
http://msdn.microsoft.com/en-us/library/2x0tte0f.aspx


ref link http://stackoverflow.com/questions/11530080/c-odbc-sql-server-2008-connection[^]


这篇关于将C ++ win32控制台应用程序连接到sql数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆