Reputation: 73
I wrote the code below to retrieve data from a database using C# Windows Form Application, but now I would like to implement the same idea of Data retrieval in VC++ MFC dialogue control: This is the code i implemented in C# Windows Form Application: Please kindly help
c.Open();
DateTime startDateTime = Convert.ToDateTime(textBox1.Text);
DateTime endDateTime = Convert.ToDateTime(textBox2.Text);
string query = "SELECT * FROM People_Tracking WHERE Enter_Exit_Time BETWEEN @startDateTime AND @endDateTime ;";
SqlCommand cmd = new SqlCommand(query, c);
cmd.Parameters.Add("@startDateTime", SqlDbType.DateTime).Value = startDateTime;
cmd.Parameters.Add("@endDateTime", SqlDbType.DateTime).Value = endDateTime;
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable t = new DataTable();
adapter.Fill(t);
dataGridView1.DataSource = t;
This is the image of how I want to display it in VC++ MFC dialogue control:
Upvotes: 0
Views: 1395
Reputation: 31599
MFC has support for ODBC with CDatabase
and CRecordset
Open Database Connectivity (ODBC)
To create the database:
SQLConfigDataSource(NULL, ODBC_ADD_DSN,
L"MICROSOFT ACCESS DRIVER (*.mdb)",
L"DSN=DataBaseName\0CREATE_DB=C:\\PATH\\DB.mdb\0\0");
//in this example "c:\\Path" must exist
To open the database:
CDatabase db;
db.Open(NULL, FALSE, FALSE,
L"ODBC;DRIVER={MICROSOFT ACCESS DRIVER (*.mdb)};DBQ=c:\\PATH\\DB.mdb");
Example of SQL commands:
db.ExecuteSQL(L"CREATE TABLE MY_TABLE (ID TEXT(4), NAME TEXT(10))");
db.ExecuteSQL(L"INSERT INTO MY_TABLE (ID, NAME) values('1000', 'MyName')");
Reading data using CRecordset
try
{
CDatabase db;
db.Open(NULL, FALSE, FALSE,
L"ODBC;DRIVER={MICROSOFT ACCESS DRIVER (*.mdb)};DBQ=c:\\PATH\\DB.mdb");
CRecordset records(&db);
records.Open(CRecordset::forwardOnly, L"SELECT * FROM MY_TABLE", CRecordset::readOnly);
while (!records.IsEOF())
{
CString id, name;
records.GetFieldValue(L"ID", id);
records.GetFieldValue(L"NAME", name);
//int index = m_listCtrl.InsertItem(0, id, 0);
//m_listCtrl.SetItemText(index, 1, name);
TRACE(L"ID = %s, Name = %s\n", id, name);
records.MoveNext();
}
db.Close();
}
catch (CDBException *e)
{
MessageBox(L"DB error: " + e->m_strError);
}
Upvotes: 6