hari
hari

Reputation: 1481

SQLserver2005 connectivty with VC++(code)

I have developed a simple application in Visual studio 2008.Its a combination of C and C++.Now am trying to connect to SQLserver2005.Installed SQlserver2005 managementExpress studio. Created database and table.To know about the connectivty took a example to insert the data from C code.But its doesnot showing any thing.I know its very simple.But some where am doing wrong. I would like to clarify few things about the below code.want to do the connectivty locally not server part by using SQLserver2005.

  1. In connection string what username and password i should mention.
  2. what does this line really do."hr = pConn->Open(strCon,"keerth","keerth",0);"
  3. ususally in MYSQL we will create the DNS.but how to mention DNS in SQL server2005.
  4. If i want to insert the variable value(which will display in console window) into database.kindly let me know is it possible? if so suggest me any idea to implement it.

Is there any tutorial links to learn about these things.

 #include "stdafx.h"

 #import "C:\Program Files\Common Files\System\ADO\msado15.dll" \
 no_namespace rename("EOF", "EndOfFile")

 int main(int argc, char* argv[])
 {

     /*The following variables will be initialized with necessary values and  appended to the strSQL values*/
     _bstr_t strName;
     _bstr_t strAge;
     _bstr_t strDOB;
     _bstr_t strSalary;

       _ConnectionPtr pConn = NULL;
     // Define string variables for ADO connection
     _bstr_t strCon("Provider=SQLOLEDB.1;Persist Security Info=False;Username=keerth;Password=keerth;Initial Catalog=keerth516;Data Source=(local);Integrated Security=SSPI;");

     HRESULT hr = S_OK;

     //Initialize the COM Library
     CoInitialize(NULL);

     try
     {
         //Create the Connection pointer
         hr = pConn.CreateInstance((__uuidof(Connection)));
         if(FAILED(hr))
         {
             printf("Error instantiating Connection object\n");
             goto cleanup;
         }

         //Open the SQL Server connection
         hr = pConn->Open(strCon,"keerth","keerth",0);
         if(FAILED(hr))
         {
              printf("Error Opening Database object using ADO _ConnectionPtr \n");
              goto cleanup;
         }

         /*Initialize the values */
        strName = "'C++ ADO insert Sample',";
        strAge = "23,";
        strDOB = "'13/04/1988',";
        strSalary = "16600.10)";

        /* Append the values to the Insert Statement */
       _bstr_t strSQL("Insert into Table1(NAME,AGE,DOB,SALARY) Values(");

        strSQL += strName + strAge + strDOB + strSalary ;

        printf("%s\n",(LPCSTR)strSQL);

        //Execute the insert statement
       pConn->Execute(strSQL,NULL,adExecuteNoRecords);

       printf("Data Added Successfully\n",(LPCSTR)strSQL);

      //Close the database
      pConn->Close();

     }
     catch(_com_error &ce)
     {
        //printf("Error:%s\n",ce.ErrorInfo);
         printf("Error:%s\n,",(char*)ce.Description());
     }


 cleanup:
     CoUninitialize();

     return 0;
 }

Upvotes: 0

Views: 492

Answers (1)

Werner Henze
Werner Henze

Reputation: 16761

You might want to take a look at the ADO API at http://msdn.microsoft.com/en-us/library/windows/desktop/ms678086(v=vs.85).aspx.

I don't think it makes sense to provide username/password as parameters to Open and as parameters in the connection string. Setting "Integrated Security=SSPI" activates integrated windows logon, so this even makes your username/password more redundant. The connectionString is documented in the MSDN (link above), the connection string parts that are specific to the DB providers are documented here: http://msdn.microsoft.com/en-us/library/windows/desktop/ms681020(v=vs.85).aspx.

"pConn->Open(strCon,"keerth","keerth",0);" opens the connection to the database server.

3.ususally in MYSQL we will create the DNS.but how to mention DNS in SQL server2005.

What do you mean with this?

4.If i want to insert the variable value(which will display in console window) into database.kindly let me know is it possible? if so suggest me any idea to implement it.

You can use ADO RecordSet with AddNew or the Execute function in your sample. Using Execute you might add adCmdText to the last parameter.

Upvotes: 1

Related Questions