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;";
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++;
}
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;";
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++;
}