Home > Software engineering >  C at the front desk after successful connection with database using SQL statements will collapse
C at the front desk after successful connection with database using SQL statements will collapse

Time:10-13

Novice, do course design, backstage database table to write good, front desk written in c + + simple console program, through the ODBC connection, the use of the SQL statement will appear the Runtime error abnormal program termination.
Below is the code, the first trigger a collapse in red mark:


#include
#include
#include
#include
#include
Using STD: : string;
using namespace std;
/*
The first step in c + + SQL database connection system configuration
1. The Settings used for the SQL server login, and users of the system security of sa to "enable" to set up the login function, and must have a password,
2. Need to be done in the ODBC data source configuration, data sources to choose \ "SQL SERVER", log in using "use input user login ID and password of the SQL SERVER authentication", (sa) and fill in the login name and password, and note that the password cannot be empty, which means your sa users must have a password, otherwise can't through the security policy system itself, the test will be finished by configuration, */

/*
C + + SQL database connection initialization step 2 c + + and SQL connection
1. In building a c + + project introduced in ADO
Specific code is as follows:
*/
# pragma warning (4146) disable:
# pragma warning (4786) disable:
# import "c: \ Program Files \ Common Files \ System \ ado \ msado15 DLL" no_namespace rename (" EOF ", "adoEOF")






//main program
Int main () {
System (COLOR "A");
BOOL FLAG=TRUE;
Int flag=0;
_ConnectionPtr m_pConnection=NULL;//the connection object 's pointer
_CommandPtr m_pCommand=NULL;//the command object 's pointer
_ParameterPtr m_pParameter=NULL;//Parameter object 's pointer
_RecordsetPtr m_pRecordset=NULL;
CoInitialize (NULL);//COM registration cannot little
Int userright=0;//user permissions, 1 for the reader, 2 for administrator
Char username [12].



/* *
_ConnectionPtr variables after the call Connection object the Open method to establish a Connection with the server,
Data types _ConnectionPtr is actually made up of class template _com_ptr_t get a specific instance of the class,
_ConnectionPtr class encapsulates the Idispatch interface pointer of Connection object and some necessary operation, can through this Connection object pointer manipulation,
* */
//connected essentially a database, for example, the code is as follows:
//connected to the MS SQL Server
//initialize the pointer
Retrieves the hr=m_pConnection. CreateInstance (__uuidof (Connection));
If (FAILED (hr))
Return 0;
//initialize the link
_bstr_t strConnect="Driver={SQL server}; Server=127.0.0.1; Uid=sasa; The PWD=123; The database=library ";//used to
//Database refers to the Database name in your system
//perform connection
Try
{
//the Open method of the connection string must be four BSTR or _bstr_t type
M_pConnection - & gt; Open (strConnect, ""," ", NULL);
}
The catch (_com_error & amp; E)
{
//MessageBox (e.D escription (), "warning", MB_OK | MB_ICONINFORMATION);
cout}//link error





/* * c + + SQL database connection step 3 simple data connection * */
//_RecordsetPtr variables, the call object you Open it, you can Open a dataset
//initialization process the following is an instance
//RecordsetPtr pRecordset;
//login
//* * *
Int loginflag=1;

/*
Enter a number, determine the reader (1) or administrator (2)
*/
Int ID=3;
Int IDflag=1;
Char password [12].
While (ID)
{
cout<" Choose identity: "& lt; Cin> ID;

/* enter the login screen readers */
If (ID==1)
{
system("cls");
cout<" Input the reader number: ";
Cin> The username;
cout<" Enter the password: ";
Cin> The password;
Char sqllogin [100].
sprintf (sqllogin, "select * from Reader where Rno='% s' and Rpassword=' % s'", username, password);
M_pRecordset=m_pConnection - & gt; Execute (_bstr_t (sqllogin), NULL, adCmdText);
the if (! M_pRecordset - & gt; AdoEOF)
{
cout<" Login successful!" ;
cout<(char *) (_bstr_t) (m_pRecordset - & gt; Fields - & gt; The GetItem (_variant_t (" uright ")) - & gt; Value);
Loginflag=0;

/* into the reader function interface */
//system (" CLS ");
Int rchose=4;
While (rchose)
{
//system (" CLS ");
cout<" 1. Query book information "& lt; cout<" 2. Query book record "& lt; cout<" 3. Quit "& lt; Cin> Rchose;
The switch (rchose)
{
Case 1:
/* */query books information
Char strsql1 [100].
sprintf (strsql1, "select * from Book order by Bno asc");
M_pRecordset=m_pConnection - & gt; Execute (_bstr_t (strsql1), NULL, adCmdText);
the if (strsql1!=NULL)
{
cout<" Book number "& lt; <" Name of the book "& lt; <" Book type "& lt; <" Book pricing "& lt; <" The book the author "& lt; Int flag=0;
while(! M_pRecordset - & gt; AdoEOF)//traversal and read the record of each column and output
{
Flag=1;
cout<(char *) (_bstr_t) m_pRecordset - & gt; The GetFields () - & gt; The GetItem (Bno) - & gt; Value<" ";
cout<(char *) (_bstr_t) m_pRecordset - & gt; The GetFields () - & gt; The GetItem (" Bname ") - & gt; Value<" ";
cout<(char *) (_bstr_t) m_pRecordset - & gt; The GetFields () - & gt; The GetItem (" Btype ") - & gt; Value<" ";
cout<(char *) (_bstr_t) m_pRecordset - & gt; The GetFields () - & gt; The GetItem (" Bprice ") - & gt; Value<" ";
cout<(char *) (_bstr_t) m_pRecordset - & gt; The GetFields () - & gt; The GetItem (" Bwriter ") - & gt; ValueM_pRecordset - & gt; MoveNext ();
}
If (flag==0)
cout<" There are no books!" }
break;
Case 2:
/* query borrowing record */
Char strsql2 [100].
Sprintf (strsql2, "select * from Record where ReRno='% s' order by Reno asc", username);
M_pRecordset=m_pConnection - & gt; Execute (_bstr_t (strsql2), NULL, adCmdText);
/* all borrowed record is the reader's */
If (strsql2!=NULL)
{
cout<" Record number "& lt; <" nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related