Report VI test database - Access via embedded SQL database

Disclaimer: This article is a blogger original article, please include the URL bloggers reprint. https://blog.csdn.net/jiayizhenzhenyijia/article/details/85088147

Word document: Portal

Embedded SQL in C language application in VC ++ 6.0, debugging in SQL Server 2000 environment can be divided into five steps:

The first step in environment initialization;

The second step precompiled;

The third step is to compile;

The fourth step is connected;

The fifth step operation.

First, the software installation and configuration environment

1. "DEVTOOLS" folder to "C: \ Program Files \ Microsoft SQL Server".

2. Initialize the VC ++ compiler environment. Run in command-line mode file "\ VC98 \ Bin \ vcvars32.bat".

3. Initialize SQLServer pre-compiler environment. Command line to run "\ Devtools \ samples \ esqlc \ setenv.bat".

4. "binn" folder contents copied to "C: \ Program Files \ Microsoft SQL Server \ MSSQL \ Binn".

Configuration 5.VC6.0 environment. The detailed configuration is divided into three steps:

①Tools-> options-> directories-> Include Files: Add C: \ Program Files \ Microsoft SQL Server \ devtools \ include. The SQL server database that comes with header files for development included in the engineering environment.

  ②Tools-> options-> directories-> Lib Files: Add C: \ Program Files \ Microsoft SQL Server \ devtools \ x861ib. The development package includes use of the project.

③Tools->options->directories->Executable Files:添加 C:\Program Files\ Microsoft SQL Server\MSSQL\Binn。 

6. Open CMD as an administrator to register, regsvr32 "VC6 \ Common \ MSDev98 \ AddIns \ FileTool.dll",

7. prompts successful, open VC6.0, right-click the toolbar, select Customize ---> Add-ins and macros file, select FileTool Developer Studio Add-in add-ons

 

2, pre-compiled

1) Copy ESQL pre-compiled files and library files

C language compiler application does not recognize SQL statements need to go through the preprocessor converted into C language. SQL Server preprocessor is nsqlprep.exe. MSSQL nsqlprep.exe in the SQL Server installation date record of the \ Binn. If the SQL Server database using either silent installation, you need to take nsqlprep.exe, SQLAIW32.DLL, SQLAKW32.DLL three files are copied to the MSSQL \ Binn directory.

2) Copy the dynamic link library SQLAKW32.dll, SQLAIW32.dll into a subdirectory under the operating system directory C: \ WINDOWS \ system32 in.

3) create an embedded SQL C source file Esql.c

#include <stdio.h>

#include <stdlib.h>

EXEC SQL BEGIN DECLARE SECTION; / * main * Variable Description Start /

char deptname[22];

char HSno[10];

char HSname[22];

char HSsex[4];

int HSage;

int NEWAGE;

long SQLCODE;

EXEC SQL END DECLARE SECTION; / * end of the description of the main variables * /

 

EXEC SQL INCLUDE sqlca; / * define the SQL communication area * /

 

/*************************************************************************/

 

int main (void) / * C language to start the main program * /

{

int count = 0;

char yn; / * variable yn behalf of yes or no * /

printf("Please choose the department name(CS/MA/IS): ");

scanf ( "% s", deptname); / * * main variable assignment deptname /

EXEC SQL CONNECT TO Student; / * database connection STUDENTDb * /

EXEC SQL DECLARE SX CURSOR FOR / * define a cursor * /

SELECT Sno, Sname, Ssex, Sage / * SX corresponding to the results statement * /

FROM Student

WHERE SDept = :deptname;

EXEC SQL OPEN SX; / * open the cursor SX will point to the first row of the query result * /

 

for (;;) / * set the processing result by one cycle of recording structure * /

{

EXEC SQL FETCH SX INTO :HSno, :HSname, :HSsex,:HSage;

      / * Advance the cursor, the current data into host variables * /

if (sqlca-> sqlcode! = 0) / * sqlcode! = 0, indicates an unsuccessful operation * /

{Printf ( "Data processing ends!");

 

BREAK; / * use status information in the SQLCA determine when to exit the loop * /}

if (count ++ == 0) / * If it is the first line, the line head hit * /

printf("\n%-10s %-22s %-4s %-10s\n", "Sno", "Sname", "Ssex", "Sage");

printf("%-10s %-22s %-4s %-10d\n", HSno, HSname, HSsex, HSage);

     / * Print the query results * /

printf ( "UPDATE AGE (y / n)?"); / * asks whether the user wants to update the student's age * /

do{                    

    scanf("%c",&yn);

}

While (a! = 'N' in &&! = 'n' && is! = 'Y' in &&! = 'y');

if (yn == 'y' || yn == 'Y') / * If the update operation selection * /

{

   printf("INPUT NEW AGE:");

   scanf ( "% d", & NEWAGE); / * user to enter a new age of the main variables * /

   EXEC SQL UPDATE Student / * Embedded SQL * /

   SET Sage = :NEWAGE

   WHERE CURRENT OF SX ;

} / * For students of all ages to update the current cursor points to * /

}

 

EXEC SQL CLOSE SX; / * Close the cursor is no longer SX and the corresponding query result * /

EXEC SQL COMMIT WORK; / * * Update submitted /

EXEC SQL DISCONNECT TEST; / * disconnect from the database * /

}

4) close the C source file MyEsql.c, and backup, and then change the source file extension ".sqc".

5) Run ESQL precompiler nsqlprep.exe program at the command line:

nsqlprep MyEsql.sqc if run successfully, generate MyEsql.c, add the file to the VC project can be compiled.

6) compile, link and run

      In the VC ++ 6.0 to create a "WIN32 Console Application" of Proiect, then the pre-compiler-generated file to the c Proiect, the compiler generates an access connection to SQL Server executable program.

 

6 . Emerging issues:

Former machine experiment, because C ++ PC installed in the D drive, precompiled environmental test initialization Visual C ++ 6.0 compiler environment and initialization of SQL Server, use the annex of the browser to run the program directly compile and run the file.

When linked to the need to create a database database name is not the first letter identifier numbers, in order to run. In the experiment can not run the program using the file attachment when the machine is running. Therefore, all operating procedures must be run from a command line mode.

   

Guess you like

Origin blog.csdn.net/jiayizhenzhenyijia/article/details/85088147