Article View: borland.public.cppbuilder.database.ado
Article #2037Formless DB access
From: Jeff Smith
Date: Mon, 17 Nov 2003 17:01
Date: Mon, 17 Nov 2003 17:01
204 lines
4173 bytes
4173 bytes
Help!
I'm moving some code to C++ Builder and a small part of the code
is database stuff and I can't get it done. The code is really
simple but since it is database stuff it is virtually impossible
to find good example code (at least for me. I spent hours looking
thru examples, online, etc.) Everything is forms and controls.
I want to do it without that stuff. Like I said, this is just
a small part of the application and I really can't have any form
or user interface stuff.
Here is the code I want in C# for a Microsoft environment. I'd
like it to be in a Borland C++ Builder environment. Can you help?
Note it does the usual (for me!) stuff. Opens/closes a db, does
a standard SQL query (Select) with parameters and sorting, inserts,
and updates. All under program control, no user interface. I left
out a lot of error checking for clarity.
//Class vars
OleDbConnection m_conn;
int m_IDate[200];
string m_UniName;
float m_avalue[200];
m_uninames[200];
int m_cnt;
int m_dbcnt;
public void OpenIt()
{
string source = @"Provider=Microsoft.Jet.OLEDB.4.0;User ID=Admin;";
source += @"Data Source=C:\xdata\";
source += "alltables.mdb;";
source += "Mode=ReadWrite;";
OleDbConnection m_conn = new OleDbConnection(source);
m_conn.Open();
}
public void CloseIt()
{
m_conn.Close();
}
public void Updateit(int j)
{
int astatus = 2;
string sSQLCommand = "UPDATE ATable SET Status = ? WHERE ";
sSQLCommand += "IDate = ? AND UniName = ? And AValue = ?";
OleDbCommand cmd;
cmd = new OleDbCommand();
cmd.CommandText = sSQLCommand;
cmd.Connection = m_conn;
cmd.Parameters.Clear();
cmd.Parameters.Add ( "Status", astatus) ;
cmd.Parameters.Add ( "IDate", m_IDate[j]) ; // int
cmd.Parameters.Add ( "UniName", m_UniName) ; // string
cmd.Parameters.Add ( "AValue", m_avalue[j]) ; // float
cmd.ExecuteNonQuery() ;
}
GetIt(int adate)
{
string selectname = "SELECT * FROM ATable Where IDate=?";
selectname += " ORDER BY UniName";
OleDbDataReader aReader;
OleDbCommand cmd;
cmd = new OleDbCommand();
cmd.CommandText = selectname;
cmd.Connection = m_conn;
cmd.Parameters.Clear();
cmd.Parameters.Add ( "IDate", adate) ;
aReader = cmd.ExecuteReader();
int j, k;
while(aReader.Read())
{
j = aReader.GetOrdinal("IDate");
k = aReader.GetInt32(j);
if (k != adate)
{
// this should not happen
continue;
}
j = aReader.GetOrdinal("UniName");
if (!aReader.IsDBNull(j))
m_uninames[m_cnt] = "";
else
m_uninames[m_cnt] = aReader.GetString(j);
j = aReader.GetOrdinal("AValue");
m_avalue[m_cnt] = aReader.GetFloat(j);
m_cnt++;
}
aReader.Close();
}
public void InsertIt(int adate, string uniname, int status, float avalue)
{
string strInsert = "INSERT INTO ATable ";
strInsert += "(Status, IDate, UniName, AValue) ";
strInsert += "VALUES (?, ?, ?, ?)";
OleDbCommand cmd;
cmd = new OleDbCommand(strInsert , m_conn);
cmd.Parameters.Add ("IDate", adate) ;
cmd.Parameters.Add ("UniName", uniname) ;
cmd.Parameters.Add ("Status", status) ;
cmd.Parameters.Add ("AValue", avalue) ;
cmd.ExecuteNonQuery() ;
m_dbcnt++;
}
Technical Details
Message-ID:
<3fb8fede$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10698b,3fe405041fb3fc6a,start X-Google-Attributes: gid10698b,public X-Google-ArrivalTime: 2003-11-17 09:10:03 PST Reply-To: "Jeff Smith" <tremblej@hotmail.com> From: "Jeff Smith" <tremblej@hotmail.com> Newsgroups: borland.public.cppbuilder.database.ado Subject: Formless DB access Date: Mon, 17 Nov 2003 12:01:16 -0500 Lines: 206 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1158 X-MIMEOLE: Produced By Microsoft MimeOLE V6.00.2800.1165 NNTP-Posting-Host: 69.140.48.66 Message-ID: <3fb8fede$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1069088478 69.140.48.66 (17 Nov 2003 09:01:18 -0700) Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail