我编写了下面的代码,使用C#Windows窗体应用程序从数据库中检索数据,但现在我想在VC ++ MFC对话控件中实现相同的数据检索思想:这是我在C#Windows窗体应用程序中实现的代码:请帮助
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;
答案 0 :(得分:6)
MFC支持使用CDatabase
和CRecordset
Open Database Connectivity (ODBC)
创建数据库:
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
打开数据库:
CDatabase db;
db.Open(NULL, FALSE, FALSE,
L"ODBC;DRIVER={MICROSOFT ACCESS DRIVER (*.mdb)};DBQ=c:\\PATH\\DB.mdb");
SQL命令示例:
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')");
使用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);
}