Question on VC++ and SQL
-
I have to make questions like this: CString abfrage; abfrage = "SELECT kstnr, kststamm.Bezeichnung, karnr, KarBezeichnung, buchungen.ktrnr, KtrBezeichnung, betrag, datum, datenart, belsymbol,\ belnr, text FROM buchungen LEFT OUTER JOIN karstamm ON buchungen.karnr = karstamm.Nummer "; abfrage = abfrage + "LEFT OUTER JOIN kststamm ON buchungen.kstnr = kststamm.Nummer "; abfrage = abfrage + " LEFT OUTER JOIN ktrstamm ON buchungen.ktrnr = ktrstamm.KtrNr WHERE datum >= "; abfrage = abfrage + "'" + kstvon.c_str() + "' AND datum <= '" + kstbis.c_str() + "'"; if (kstnr != "") abfrage = abfrage + " AND kstnr = '" + kstnr.c_str() + "'"; if (koarnr != "") abfrage = abfrage + " AND karnr = '" + koarnr.c_str() + "'"; if (datenart != "") abfrage = abfrage + " AND datenart = '" + datenart.c_str() + "'"; rs->Close(); rs->Open(CRecordset::snapshot, abfrage); Is there an easier way to make a Select in VC++?? I made this question in the Longe, unforounatly - and my question was even deleted (what's right, wrong click;)) But can anyone help here??? Thanks, Gerhard
-
I have to make questions like this: CString abfrage; abfrage = "SELECT kstnr, kststamm.Bezeichnung, karnr, KarBezeichnung, buchungen.ktrnr, KtrBezeichnung, betrag, datum, datenart, belsymbol,\ belnr, text FROM buchungen LEFT OUTER JOIN karstamm ON buchungen.karnr = karstamm.Nummer "; abfrage = abfrage + "LEFT OUTER JOIN kststamm ON buchungen.kstnr = kststamm.Nummer "; abfrage = abfrage + " LEFT OUTER JOIN ktrstamm ON buchungen.ktrnr = ktrstamm.KtrNr WHERE datum >= "; abfrage = abfrage + "'" + kstvon.c_str() + "' AND datum <= '" + kstbis.c_str() + "'"; if (kstnr != "") abfrage = abfrage + " AND kstnr = '" + kstnr.c_str() + "'"; if (koarnr != "") abfrage = abfrage + " AND karnr = '" + koarnr.c_str() + "'"; if (datenart != "") abfrage = abfrage + " AND datenart = '" + datenart.c_str() + "'"; rs->Close(); rs->Open(CRecordset::snapshot, abfrage); Is there an easier way to make a Select in VC++?? I made this question in the Longe, unforounatly - and my question was even deleted (what's right, wrong click;)) But can anyone help here??? Thanks, Gerhard
Your question is not 100% on this forum again;) anyway I forgot sql server but I think its possible can you create a view on your database and use of it on the VC++?and also you can use of CString::Insert instead abfrage = abfrage + ...;)
WhiteSky
-
I have to make questions like this: CString abfrage; abfrage = "SELECT kstnr, kststamm.Bezeichnung, karnr, KarBezeichnung, buchungen.ktrnr, KtrBezeichnung, betrag, datum, datenart, belsymbol,\ belnr, text FROM buchungen LEFT OUTER JOIN karstamm ON buchungen.karnr = karstamm.Nummer "; abfrage = abfrage + "LEFT OUTER JOIN kststamm ON buchungen.kstnr = kststamm.Nummer "; abfrage = abfrage + " LEFT OUTER JOIN ktrstamm ON buchungen.ktrnr = ktrstamm.KtrNr WHERE datum >= "; abfrage = abfrage + "'" + kstvon.c_str() + "' AND datum <= '" + kstbis.c_str() + "'"; if (kstnr != "") abfrage = abfrage + " AND kstnr = '" + kstnr.c_str() + "'"; if (koarnr != "") abfrage = abfrage + " AND karnr = '" + koarnr.c_str() + "'"; if (datenart != "") abfrage = abfrage + " AND datenart = '" + datenart.c_str() + "'"; rs->Close(); rs->Open(CRecordset::snapshot, abfrage); Is there an easier way to make a Select in VC++?? I made this question in the Longe, unforounatly - and my question was even deleted (what's right, wrong click;)) But can anyone help here??? Thanks, Gerhard
Try the CString::Format member. Here is what with ADO and MFC:
CString tempStr;
try {
tempStr.Format("SELECT * "
"FROM CAD_Info "
"WHERE ( ([SessionNumber] = %d) "
"AND ([SiteID] = %d) "
"AND ([SeqNumber] = %d) )",
nSession,nSiteID,nSeqNum);CMainDataBase::TESTHR(pRst.CreateInstance(\_\_uuidof(Recordset))); CADORecordSetEx rst(m\_pDB,pRst); rst.Open(tempStr,m\_cursorType,m\_lockType, adCmdText); // Do something with the recordset...
}
catch(CException* e)
{
// Do something with the error
}
catch (...)
{}
I know you are using CRecordSet and DAO but the syntax should be similar.
John
-
Try the CString::Format member. Here is what with ADO and MFC:
CString tempStr;
try {
tempStr.Format("SELECT * "
"FROM CAD_Info "
"WHERE ( ([SessionNumber] = %d) "
"AND ([SiteID] = %d) "
"AND ([SeqNumber] = %d) )",
nSession,nSiteID,nSeqNum);CMainDataBase::TESTHR(pRst.CreateInstance(\_\_uuidof(Recordset))); CADORecordSetEx rst(m\_pDB,pRst); rst.Open(tempStr,m\_cursorType,m\_lockType, adCmdText); // Do something with the recordset...
}
catch(CException* e)
{
// Do something with the error
}
catch (...)
{}
I know you are using CRecordSet and DAO but the syntax should be similar.
John
Thanks for the answer, Unfortunatly I have no experience with ADO, but I have some books to learn it. Maybe I have to do so now;) BTW, the statemant above ment, this is not the rigth forum too. What would be the right one. In the Lounge (:-O sorry again), you said, I should try in the MFC-forum, but I can't find. Thanks, Gerhard
-
Your question is not 100% on this forum again;) anyway I forgot sql server but I think its possible can you create a view on your database and use of it on the VC++?and also you can use of CString::Insert instead abfrage = abfrage + ...;)
WhiteSky
-
Tahnks, Seems to be a good idea. Can you tell me, what would be the right forum for a question like this. Lounge was a fault, of course:-O - sorry. Gerhard
Your're welcome Well Lounge was 100% fault.No programming questions please (on the Lounge);) Why you cant find MFC forum (Visual C++ / MFC) Visual C++ / MFC[^]
WhiteSky
-
Thanks for the answer, Unfortunatly I have no experience with ADO, but I have some books to learn it. Maybe I have to do so now;) BTW, the statemant above ment, this is not the rigth forum too. What would be the right one. In the Lounge (:-O sorry again), you said, I should try in the MFC-forum, but I can't find. Thanks, Gerhard
ensger wrote:
this is not the rigth forum too. What would be the right one.
1: Querying the database in the manor you are, your exposing the database to SQL injection attacks. See this article for help distinguishing these attacks. SQL Injection Attacks[^] 2: Looks like you where suggested to ask this question in the MFC forum was because it’s all string manipulation. Which falls under the purview of MFC CString, although for the purposes here it’s really fine in either forum.
ensger wrote:
I should try in the MFC-forum, but I can't find.
Visual C++ / MFC[^] Good Luck
I'd love to help, but unfortunatley I have prior commitments monitoring the length of my grass. :Andrew Bleakley:
-
Your're welcome Well Lounge was 100% fault.No programming questions please (on the Lounge);) Why you cant find MFC forum (Visual C++ / MFC) Visual C++ / MFC[^]
WhiteSky
-
ensger wrote:
this is not the rigth forum too. What would be the right one.
1: Querying the database in the manor you are, your exposing the database to SQL injection attacks. See this article for help distinguishing these attacks. SQL Injection Attacks[^] 2: Looks like you where suggested to ask this question in the MFC forum was because it’s all string manipulation. Which falls under the purview of MFC CString, although for the purposes here it’s really fine in either forum.
ensger wrote:
I should try in the MFC-forum, but I can't find.
Visual C++ / MFC[^] Good Luck
I'd love to help, but unfortunatley I have prior commitments monitoring the length of my grass. :Andrew Bleakley:
-
Becouse I opened the menue-point 'Message Boards' and searched there. And there it's called Visual C++ only. But I already found, thanks
Not problem maybe you are new here(codeproject),but I am ready to help people ;)
WhiteSky
-
Thanks for the answer, Unfortunatly I have no experience with ADO, but I have some books to learn it. Maybe I have to do so now;) BTW, the statemant above ment, this is not the rigth forum too. What would be the right one. In the Lounge (:-O sorry again), you said, I should try in the MFC-forum, but I can't find. Thanks, Gerhard
ensger wrote:
nfortunatly I have no experience with ADO, but I have some books to learn it.
I recommend ADO if you plan on writing SQL code for connecting to a MS SQL server (or the free version of that) as it is much better at that task and it works well with multithreading which with DAO you have to be very careful. Although I do not use this code I recommend this class for connecting to ADO databases: http://www.codeproject.com/database/caaadoclass1.asp[^]
John