Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of operating sqlserver Database in C language

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "the method of operating sqlserver database in C language". The content of the explanation in this article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the method of operating sqlserver database in C language".

Operating system: windows 10 Experimental platform: vs2012 + sql server 2008

Introduction to ODBC: open Database connection (Open Database Connectivity,ODBC), the main function is to provide a set of programming interfaces for database access, its main feature is that if the application uses ODBC as the data source, then the application is independent of the database or database engine used, laying the foundation for the cross-platform and portability of the application.

Create ODBC data sources: control Panel-Administrative tools-ODBC data sources (32-bit)

As you can see, there are three types of DSN (data source names), among which:

User DSN: only users who created the DSN are allowed to use this data source

System DSN: all users who log in to the server can use this data source

File DSN: configuration information is saved in the file and can be used by all logged-in users

In this lab, the system DSN:

Click add, and then select SQL Server

Then enter the name of the data source and the server where it is located. Our database is installed on this machine.

Select to authenticate as a sql server user, as shown in the following figure:

Here you can set the database to which you connect by default, leave it unchanged, and use master as the default database.

Then click next, and then finish, and then you can test it.

This completes the creation of the ODBC data source.

In the program test, the database already contains a database called stu_info, and there is a student student information table, which contains the following field information:

The program is designed as follows

# include#include#include#include#includeint main () {SQLRETURN ret; SQLHENV henv; SQLHDBC hdbc; SQLHSTMT hstmt; ret=SQLAllocHandle (SQL_HANDLE_ENV,NULL,&henv); / / Application environment handle ret=SQLSetEnvAttr (henv,SQL_ATTR_ODBC_VERSION, (SQLPOINTER) SQL_OV_ODBC3,SQL_IS_INTEGER); ret=SQLAllocHandle (SQL_HANDLE_DBC,henv,&hdbc) / / apply for database connection handle ret=SQLConnect (hdbc, (SQLCHAR*) "data_test", SQL_NTS, (SQLCHAR*) "sa", SQL_NTS, (SQLCHAR*) "12345678", SQL_NTS) / * data_test is the name of the configured ODBC data source. Modify * / if (! (ret==SQL_SUCCESS | | ret==SQL_SUCCESS_WITH_INFO)) {printf ("failed to connect to the database!"); return-1;} ret=SQLAllocHandle (SQL_HANDLE_STMT,hdbc,&hstmt) according to your own configuration / * execute sql statement * / SQLCHAR sql1 [] = "use stu_info"; SQLCHAR sql2 [] = "select * from student"; ret=SQLExecDirect (hstmt,sql1,SQL_NTS); ret=SQLExecDirect (hstmt,sql2,SQL_NTS) If (ret==SQL_SUCCESS | | ret==SQL_SUCCESS_WITH_INFO) {SQLCHAR str1 [50], str2 [50], str3 [50], str4 [50], str5 [50], str6 [50]; SQLINTEGER len_str1, len_str2, len_str3, len_str4, len_str5, len_str6 While (SQLFetch (hstmt)! = SQL_NO_DATA) {SQLGetData (hstmt,1,SQL_C_CHAR,str1,50,&len_str1); / / get the first column of data SQLGetData (hstmt,2,SQL_C_CHAR,str2,50,&len_str2) SQLGetData (hstmt,3,SQL_C_CHAR,str3,50,&len_str3); SQLGetData (hstmt,4,SQL_C_CHAR,str4,50,&len_str4); SQLGetData (hstmt,5,SQL_C_CHAR,str5,50,&len_str5); SQLGetData (hstmt,6,SQL_C_CHAR,str6,50,&len_str6) Printf ("% s% s", str1,str2,str3,str4,str5,str6);}} SQLFreeHandle (SQL_HANDLE_DBC,hdbc); / / release connection handle SQLFreeHandle (SQL_HANDLE_ENV,henv); / / release environment handle return 0;}

It is important to note that in most environments, the code will report an error and the error message is as follows: (SQLCHAR*) is not compatible with (SQLWCHAR*), so some people may think that it will be forced to switch to (SQLWCHAR*) directly, but still report an error at the SQLConnect function and cannot connect to the database. In this case, you need to modify the character set and configure it to multi-byte character set, as follows:

Thank you for your reading, the above is the content of "the method of operating sqlserver database in C language". After the study of this article, I believe you have a deeper understanding of the method of operating sqlserver database in C language, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report