Embedded SQL statements in the C language

Source: Internet
Author: User
Tags mssql

Embedded SQL statements in the C language: "Reprint"

Http://blog.csdn.net/cnlht/archive/2007/12/12/1930960.aspx Original Address

Experimental content:

Master the use of the pre-compiled program NSQLPREP.EXE SQL Server 2000 (debug with textbook example 1);

Experimental steps:

First, the database environment configuration

1, create Xuesheng database, establish student table, etc.

2. Close SQL Server 2000 Service Manager;

3. Copy the Devtools folder to: C:\Program Files\Microsoft SQL Server

4. Copy the Binn folder to: C:\Program Files\Microsoft SQL Server\MSSQL

5, start the server;

Second, vc++6.0 Editor configuration (Initialize VC + + Environment)

1. Tools, select Directory->include Files

Added: C:\Program Files\Microsoft SQL Server\devtools\include

and set as the first item

2. Select Library Files

Added: C:\Program Files\Microsoft SQL Server\devtools\x86lib

and set as the first item

Third, write the program, pre-compile, and finally in VC + + compile, execute

1. Edit Exec.sqc file, save to: C:\Program Files\Microsoft SQL Server\mssql\binn Directory

The EXEC.SQC file is as follows:

#include <stdio.h>
#include <stdlib.h>


EXEC SQL BEGIN DECLARE section; /* Main variable description start */
Char deptname[64];
Char hsno[64];
Char hsname[64];
Char hssex[64];
int hsage;
int newage;
Long SQLCODE;
EXEC SQL END DECLARE section; /* Main variable description end */

EXEC SQL INCLUDE Sqlca; /* Define the SQL Communication area */

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

int main (void)                            /*c Language Main program start */
{
int count = 0;
Char yn;                              /* Variable yn represents yes or no*/
printf ("please Choose the department name (cs/ma/is): ");
scanf ("%s", Deptname);                 /* Deptname Assignment for primary variable */
EXEC SQL CONNECT to hp-08d6cxf128b\sql2000 USER sa;     & nbsp;  /* Connect database test*/
EXEC SQL DECLARE SX cursor for  /* Define CURSOR */
SELECT Sno, Sname, Ssex, SAGE&N bsp; /*SX The execution result of the corresponding statement */
from Student
WHERE sdept =:d eptname;
EXEC SQL Open sx;       /* Opens the cursor SX and points to the first line of the query result */

for (;;) /* Process the records of the result set individually with a looping structure */
{
EXEC SQL FETCH SX into:hsno,: hsname,: hssex,:hsage;
/* Push the cursor to put the current data into the main variable */
if (Sqlca->sqlcode! = 0)/* sqlcode! = 0, indicating that the operation was unsuccessful */
Break /* Use status information in Sqlca to decide when to exit the loop */
if (count++ = = 0)/* If it is the first line, first hit the head */
printf ("\n%-10s%-20s%-10s%-10s\n", "Sno", "Sname", "Ssex", "Sage");
printf ("%-10s%-20s%-10s%-10d\n", Hsno, Hsname, Hssex, hsage);
/* Print Query Results */
printf ("UPDATE Age (y/n)?"); /* Ask the user if they want to update the student's age */
do{
scanf ("%c", &yn);
}
while (yn! = ' n ' && yn! = ' n ' && yn! = ' y ' && yn! = ' y ');
if (yn = = ' Y ' | | yn = = ' y ')/* If you select Update Action */
{
printf ("INPUT NEW Age:");
scanf ("%d", &newage); /* user enters New age into main variable */
EXEC SQL UPDATE Student/* Embedded sql*/
SET Sage =: newage
WHERE Current of SX;
}/* Update the age of the student pointing to the current cursor */
}

EXEC SQL CLOSE SX; /* Close cursor SX no longer corresponds to query result */
EXEC SQL COMMIT work; /* Submit UPDATE */
EXEC SQL DISCONNECT TEST; /* Disconnect the database connection */
}

2. Pre-compiling with MS-DOS:

1) into the Binn file: cd C:\Program Files\Microsoft SQL Server\mssql\binn

2) Call the precompiled Program Nsqlprep.exe program: Nsqlprep EXEC

Successful compilation completion Result:

See annex 1

3, use VC + + to compile, see annex 2

4. Add library File: Project---Set->link-> object/library module
Enter SQLakw32.lib Caw32.lib two file name. Annex 3
Note that two files are separated by a space.
5. Copy the dynamic link library Sqlakw32.dll,sqlaiw32.dll file to the subdirectory under the operating system directory C:\WINDOWS\system32

6. Connection: Annex 4

7. Implementation: Annex 5

Annex 1:

Embedded SQL statements in the C language

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.