Okay... I am new into the world of C++ and am in need of assistance.
Application Goal Command Line App to parse through a large ascii text file, glean data, then insert data into a database.
EnvironmentMicrosoft Visual C++ 2005
MS SQL Server 2005
Database InformationServer: MSSQL
Database: Portal
Username: PortalUser
Password: Password
Table (for example) can be defned as:
MyTable
UID int,
Field1 varchar(50),
Data1 text
Where UID is an auto generated ID field.
Field1 is a varchar field where it will insert a text sting from gleaned data.
Data1 is a text field where it will insert the section of text the string was gleaned from.
I.E. Insert into MyTable (Field1, Data1) values ('<gleaned text>','<all text>')
Gleaned text in field1 is a string: "Chemestry"
All text for Data1 is a string: "WorkCenter 00100 Chemestry yada yada yada it goes on and on"
I have created a new project using a CLR command line project.
I am close, but not sure how to proceed. I could really use a hand with a conceptual model to insert into the SQL DB.
I'm pretty sure the concept is to set up a connection, then execute a command using that connection.
I have the app coded so that it will not connect unless run with a command line flag. I.E. MyApp.exe /T <-- This will move to a condition where it will do the SQL connectivity then exit. Once I have it working, I will integrate that into the proper places of the app.
Unfortunatly I cannot find a base level example that I can get to work for me. Everything is geared at C++ devs with higher level knowledge than I.
Really I am porting this app from a PHP function. Everything works fine in PHP, it was just too much processing for the web server to deconstruct the ASCII file. I am moving it to C++ (per suggestion of a colleage) as my entry into this lanuage.
All and all I am quite happy with the performance, I just cannot get the last bit where I tell it to connect to the SQL box and execute a command.
I'd be happy to provide all of my code, but the majority does not apply, instead I will supply the bits that are important. It should be understandable.
CODE
//required for the rest of the app
#include <fstream>
#include <iostream>
#include <string>
#include <cctype>
#include <stdlib.h>
//added while trying to get this working, not sure what is really needed
#using <mscorlib.dll>
#using <System.dll>
#using <System.Data.dll>
//Gets down into the program, and in the condition that runs the SQL connection.
SQL.CommandText = "insert into MyTable (Field1, Data1) values (Gleaned, Full)";
sqlConnection1.ConnectionString "Server=MSSQL; database=Potal; UID=PortalUser;PWD=Password";
So can you throw me a bone and show me how to execute this? I'm seriously at the end of my rope and am running up against deadlines.
(FYI - This code is going to be used to populate a database for Q/A of laboratory data. There is no revenue associated to the delivery of this code. This is for internal use in my department.)
Here is the complete code:
CODE
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
// WARloader.cpp : main project file.
#include "stdafx.h"
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#include <stdio.h>
#include <tchar.h>
#include <fstream>
#include <iostream>
#include <string>
#include <cctype>
#include <stdlib.h>
#using <mscorlib.dll>
#using <System.dll>
#using <System.Data.dll>
using namespace System;
using namespace System::Data;
using namespace System::Data::SqlClient;
int main(int argc,char *argv[])
{
//vars that will come from config
char TargetFile[]="import.war";
//declare vars for reading file
std::string Import;
std::string WholeReport;
std::string line;
std::ifstream WARreport;
//declare vars for pagination
std::string::size_type EoF;
std::string Page;
std::string::size_type PageLen;
//declare vars for flags
std::string Dev;
std::string DevInfo;
std::string Help;
std::string HelpInfo;
std::string Current;
//declare vars for Page Breaks
std::string::size_type PageBreak1;
std::string::size_type PageBreak2;
//declare vars for Page Numbers
std::string::size_type PageFinderStart;
std::string::size_type PageFinderEnd;
std::string PageNumber;
//declare vars for Work Center
std::string::size_type WorkCenterFinderStart;
std::string::size_type WorkCenterFinderEnd;
std::string WorkCenter;
std::string WorkCenterNumber;
std::string WorkCenterName;
//declare vars for Report Date
std::string::size_type ReportDateStart;
std::string::size_type ReportDateEnd;
std::string ReportDate;
std::string ReportMonth;
std::string ReportDay;
std::string ReportYear;
//declare vars for SQL inserts
SqlCommand SQL;
SqlConnection sqlConnection1;
SqlDataAdapter sqlDataAdapter1;
//Place Holder Conditions
EoF = -1;
Page = "WAR Import Utility\n\nJohn A Hamilton is a hoopy frood who always knows where his towel is.";
Dev = "D";
DevInfo = Page;
Help = "H";
HelpInfo = "WAR Import Utility\n\nThis utility will import the WAR report into the PATH-SQL PathPortal database.";
for(int i=0; i < argc; i++) {
Current = argv[i];
Current = Current.substr(1);
for(unsigned int i=0;i<Current.length();i++) {
Current[i] = toupper(Current[i]);
}
if (Current == Dev) {
std::cout << DevInfo << std::endl;
return 0;
}
if (Current == Help) {
std::cout << HelpInfo << std::endl;
return 0;
}
if (Current == Help) {
std::cout << HelpInfo << std::endl;
return 0;
}
if (Current == "T") {
std::cout << "WAR Import Utility\n\nDB Test Flag Received" << std::endl;
SQL.CommandText = "Select * from ppUsers";
sqlConnection1.ConnectionString = "Server=??????; database=?????; UID=?????;PWD=?????";
Cant get this to go. :(
???????????????
???????????????
???????????????
return 0;
}
}
WARreport.open(TargetFile);
if (!WARreport) {
std::cout << "Unable to open file: " << std::endl;
exit(1); // terminate with error
}
std::cout << "WAR Import Utility\n\n---Starting File Import---" << std::endl;
while(std::getline(WARreport,line)) {
WholeReport += line;
}
std::cout << "---File Import Finished---\n\n\n\n---Begin Processing---\n" << std::endl;
while (EoF == -1) {
//Find the next page
PageBreak1 = WholeReport.find("REPORT NO: BP-WAR");
PageBreak2 = WholeReport.find("REPORT NO: BP-WAR",10);
if (PageBreak2 == -1) { PageBreak2 = WholeReport.length(); }
Page = WholeReport.substr(PageBreak1,PageBreak2);
PageLen = Page.length();
WholeReport = WholeReport.replace(PageBreak1,PageLen,"");
EoF = Page.find("* * * E N D O F R E P O R T * * *");
//glean page number
PageFinderStart = Page.find("PAGE NO:");
PageFinderEnd = Page.find("WORKCENTER");
PageNumber = Page.substr(PageFinderStart + 8,PageFinderEnd - PageFinderStart - 8);
//remove white space
PageFinderStart = PageNumber.find_first_not_of(" ");
PageFinderEnd = PageNumber.find_last_not_of(" ");
PageNumber = PageNumber.substr(PageFinderStart,PageFinderEnd-PageFinderStart+1);
//glean report date
ReportDateStart = Page.find("AS OF DATE:");
ReportDateEnd = Page.find("PREPARED:");
ReportDate = Page.substr(ReportDateStart + 11,ReportDateEnd-ReportDateStart-11);
//remove white space
ReportDateStart = ReportDate.find_first_not_of(" ");
ReportDateEnd = ReportDate.find_last_not_of(" ");
ReportDate = ReportDate.substr(ReportDateStart,ReportDateEnd-ReportDateStart+1);
//Date to Day, Month, Year
ReportDay = ReportDate.substr(0,2);
ReportMonth = ReportDate.substr(2,3);
ReportYear = ReportDate.substr(5,2);
//glean WorkCenter
WorkCenterFinderStart = Page.find("WORKCENTER");
WorkCenter = Page.substr(WorkCenterFinderStart+12,100);
WorkCenterNumber = WorkCenter.substr(0,5);
WorkCenterName = WorkCenter.substr(6);
//remove white space
WorkCenterFinderStart = WorkCenterName.find_first_not_of(" ");
WorkCenterFinderEnd = WorkCenterName.find_last_not_of(" ");
WorkCenterName = WorkCenterName.substr(WorkCenterFinderStart,WorkCenterFinderEnd-WorkCenterFinderStart+1);
std::cout << "Report Date: " << ReportMonth << " " << ReportDay << ", 20" << ReportYear << "\nPage: " << PageNumber <<"\nWorkCenter Num: " << WorkCenterNumber << "\nWorkCenter Name: " << WorkCenterName << "\n" << std::endl;
}
std::cout << "---End Processing---" << std::endl;
return 0;
}
This post has been edited by YoYo-Pete: 6 Feb, 2008 - 12:34 PM