我想用C ++ .net连接到sqlServer [英] I wan to connect to sqlServer with C++.net

查看:102
本文介绍了我想用C ++ .net连接到sqlServer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想连接sqlServer和C ++ .net。

请给我样品。

谢谢。

解决方案

参考:使用托管C ++的ADO.NET入门 [ ^ ]


使用C ++ ODBC连接到SQL Server示例

目标

此Windows C ++教程的目的是连接到SQL Server数据库使用本机C ++并执行简单查询。我们将使用带有DRIVER的连接字符串而不是DSN。这是一个相当简单的C ++示例,我们将使用Windows命令行编译器cl.exe。虽然它很简单,但它演示了使用C ++和SQL DB连接和检索数据所需的内容,以及一些错误处理。用户可以深入探索这些功能。请查看C ++中ODBC连接池的SQL Server ODBC C ++连接池。留下任何问题或问题作为评论,我会尽力回答。



假设

本文假设您已安装并配置了Microsoft SQL Server 2008 Express Edition。有关更多详细信息,请参阅安装和配置SQL Server 2008。另请注意,SQL Server默认情况下未启用端口1433。请参阅前面提到的链接,了解如何执行此操作。





此示例中使用的版本

软件/组件映像

Windows XP SP2 N / A

VC ++ 2008快速版N / A

SQL Server 2008 Express Edition N / A

可在此处找到这些文件的链接





编写并编译示例

编写客户端代码并将其保存为MicrosoftSQLExample.cpp

1. #include< iostream>

2. #include< windows.h>

3. #include< sqltypes.h>

4. #include< sql.h>

5. #include< sqlext.h>

6.

7.使用命名空间std;

8.

9. void show_error(unsigned int handletype ,const SQLHANDLE& handle){

10. SQLCHAR sqlstate [1024];

11. SQLCHAR消息[1024];

12.如果(SQL_SUCCESS == SQLGetDiagRec(handletype,handle,1,sqlstate,NULL,messa) ge,1024,NULL))

13. cout<<消息:<< message<<\ nSQLSTATE:<<< sqlstate<< endl; < br $>
14.}

15.

16. int main(){

17.

18. SQLHANDLE sqlenvhandle;

19. SQLHANDLE sqlconnectionhandle;

20. SQLHANDLE sqlstatementhandle;

21. SQLRETURN retcode;

22.

23. if(SQL_SUCCESS!= SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,& sqlenvhandle))

24. goto FINISHED;

25.

26. if(SQL_SUCCESS!= SQLSetEnvAttr(sqlenvhandle,SQL_ATTR_ODBC_VERSION,(SQLPOINTER)SQL_OV_ODBC3,0))

27. goto FINISHED;

28。

29. if(SQL_SUCCESS!= SQLAllocHandle(SQL_HANDLE_DBC,sqlenvhandle,& sqlconnectionhandle))

30. goto FINISHED;

31.

32. SQLCHAR retconstring [1024];

33.开关(SQLDriverConnect(sqlconnectionhandle,

34. NULL,

35.(SQLCHAR *)DRIVER = {SQL Server}; SERVER = localhost,1433; DATABASE = MyDatabase; UID = sa; PWD = Admin-123;,

36. SQL_NTS,

37. r etconstring,

38. 1024,

39. NULL,

40. SQL_DRIVER_NOPROMPT)){

41。 case SQL_SUCCESS_WITH_INFO:

42. show_error(SQL_HANDLE_DBC,sqlconnectionhandle);

43. break;

44. case SQL_INVALID_HANDLE:

45. case SQL_ERROR:

46. show_error(SQL_HANDLE_DBC,sqlconnectionhandle);

47. goto FINISHED;

48.默认值:

49.休息;

50.}

51.

52. if(SQL_SUCCESS!= SQLAllocHandle( SQL_HANDLE_STMT,sqlconnectionhandle,& sqlstatementhandle))

53. goto FINISHED;

54.

55. if(SQL_SUCCESS!= SQLExecDirect(sqlstatementhandle) ,(SQLCHAR *)select * from testtable,SQL_NTS)){

56. show_error(SQL_HANDLE_STMT,sqlstatementhandle);

57. goto完成;

58.}

59.否则{

60. char name [64];

61 .char地址[64];

62. int id;

63. while(SQLFetch(sqlstatementhandle)== SQL_SUCCESS){

64 .SQLGetData(sqlstatementhandle,1,SQL_C_ULONG,& id,0,NULL);

65. SQLGetData(sqlstatementhandle,2,SQL_C_CHAR,name,64,NULL);

66. SQLGetData(sqlstatementhandle,3,SQL_C_CHAR,address,64,NULL);

67. cout<< id<<<< name<<< < address<< endl;

68.}

69.}

70.

71。完成:

72. SQLFreeHandle(SQL_HANDLE_STMT,sqlstatementhandle);

73. SQLDisconnect(sqlconnectionhandle);

74. SQLFreeHandle(SQL_HANDLE_DBC,sqlconnectionhandle) ;

75. SQLFreeHandle(SQL_HANDLE_ENV,sqlenvhandle);

76.

77.}

隐藏行号



现在打开一个promt到你保存文件的位置并编译它。只是提醒您确保事先运行vcvars32或者您将遇到各种问题。



MicrosoftSQLExample> cl MicrosoftSQLExample.cpp odbc32.lib


关于 12,000,000 Google上的结果:Sql server C ++ .NET [ ^ ]。

I wan to connect sqlServer with C++.net.
please give me sample.
thank you .

解决方案

Refer: ADO.NET Primer using Managed C++[^]


Connecting To SQL Server Using C++ ODBC Example
Aim
The aim of this Windows C++ tutorial is to connect to an SQL Server database using native C++ and perform a simple query. We will use a connection string with a DRIVER and not a DSN . This is a fairly simple C++ example and we will use the Windows command line compiler cl.exe. While it is simple, it demonstrates what is required for connecting and retrieving data using C++ and SQL DB, with some error handling too. It is upto the user to furthere explore these functions. Please have a look at SQL Server ODBC C++ Connection Pool for an ODBC connection pool in C++. Leave any questions or problems as comments and I will endeavour to answer them.

Assumptions
This article assumes that you have Microsoft SQL Server 2008 Express edition installed and configured. Please see Installing and Configuring SQL Server 2008 for more details. Also note that SQL Server does not have port 1433 enabled by default. Please see the afore mentioned link on how to do this.


Versions used in this example
Sofware/Component Image
Windows XP SP2 N/A
VC++ 2008 express Edition N/A
SQL Server 2008 Express Edition N/A
Links to these files can be found here


Write and Compile the Example
Write the client code and save it as MicrosoftSQLExample.cpp
1. #include <iostream>
2. #include <windows.h>
3. #include <sqltypes.h>
4. #include <sql.h>
5. #include <sqlext.h>
6.
7. using namespace std;
8.
9. void show_error(unsigned int handletype, const SQLHANDLE& handle){
10. SQLCHAR sqlstate[1024];
11. SQLCHAR message[1024];
12. if(SQL_SUCCESS == SQLGetDiagRec(handletype, handle, 1, sqlstate, NULL, message, 1024, NULL))
13. cout<<"Message: "<<message<<"\nSQLSTATE: "<<sqlstate<<endl;
14. }
15.
16. int main(){
17.
18. SQLHANDLE sqlenvhandle;
19. SQLHANDLE sqlconnectionhandle;
20. SQLHANDLE sqlstatementhandle;
21. SQLRETURN retcode;
22.
23. if(SQL_SUCCESS!=SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &sqlenvhandle))
24. goto FINISHED;
25.
26. if(SQL_SUCCESS!=SQLSetEnvAttr(sqlenvhandle,SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, 0))
27. goto FINISHED;
28.
29. if(SQL_SUCCESS!=SQLAllocHandle(SQL_HANDLE_DBC, sqlenvhandle, &sqlconnectionhandle))
30. goto FINISHED;
31.
32. SQLCHAR retconstring[1024];
33. switch(SQLDriverConnect (sqlconnectionhandle,
34. NULL,
35. (SQLCHAR*)"DRIVER={SQL Server};SERVER=localhost, 1433;DATABASE=MyDatabase;UID=sa;PWD=Admin-123;",
36. SQL_NTS,
37. retconstring,
38. 1024,
39. NULL,
40. SQL_DRIVER_NOPROMPT)){
41. case SQL_SUCCESS_WITH_INFO:
42. show_error(SQL_HANDLE_DBC, sqlconnectionhandle);
43. break;
44. case SQL_INVALID_HANDLE:
45. case SQL_ERROR:
46. show_error(SQL_HANDLE_DBC, sqlconnectionhandle);
47. goto FINISHED;
48. default:
49. break;
50. }
51.
52. if(SQL_SUCCESS!=SQLAllocHandle(SQL_HANDLE_STMT, sqlconnectionhandle, &sqlstatementhandle))
53. goto FINISHED;
54.
55. if(SQL_SUCCESS!=SQLExecDirect(sqlstatementhandle, (SQLCHAR*)"select * from testtable", SQL_NTS)){
56. show_error(SQL_HANDLE_STMT, sqlstatementhandle);
57. goto FINISHED;
58. }
59. else{
60. char name[64];
61. char address[64];
62. int id;
63. while(SQLFetch(sqlstatementhandle)==SQL_SUCCESS){
64. SQLGetData(sqlstatementhandle, 1, SQL_C_ULONG, &id, 0, NULL);
65. SQLGetData(sqlstatementhandle, 2, SQL_C_CHAR, name, 64, NULL);
66. SQLGetData(sqlstatementhandle, 3, SQL_C_CHAR, address, 64, NULL);
67. cout<<id<<" "<<name<<" "<<address<<endl;
68. }
69. }
70.
71. FINISHED:
72. SQLFreeHandle(SQL_HANDLE_STMT, sqlstatementhandle );
73. SQLDisconnect(sqlconnectionhandle);
74. SQLFreeHandle(SQL_HANDLE_DBC, sqlconnectionhandle);
75. SQLFreeHandle(SQL_HANDLE_ENV, sqlenvhandle);
76.
77. }
Hide line numbers

Now open a promt to where you saved the file and compile it. Just a reminder to make sure you have run vcvars32 beforehand or you will have all kinds of issues.

MicrosoftSQLExample>cl MicrosoftSQLExample.cpp odbc32.lib


About 12,000,000 results on Google: "Sql server C++ .NET"[^].


这篇关于我想用C ++ .net连接到sqlServer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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