mfc连接sql server代码
时间: 2023-02-22 20:57:11 浏览: 62
这是一段使用 MFC 连接 SQL Server 的示例代码:
```
#include <afxdb.h>
#include <iostream>
int main()
{
try
{
CDatabase database;
database.OpenEx(_T("ODBC;DSN=your_dsn_name"), CDatabase::noOdbcDialog);
CString strSQL;
strSQL.Format(_T("SELECT * FROM your_table"));
CRecordset recset(&database);
recset.Open(CRecordset::forwardOnly, strSQL, CRecordset::readOnly);
CString strValue;
while (!recset.IsEOF())
{
recset.GetFieldValue(_T("your_column"), strValue);
std::cout << CT2A(strValue) << std::endl;
recset.MoveNext();
}
recset.Close();
database.Close();
}
catch (CDBException* e)
{
AfxMessageBox(e->m_strError);
e->Delete();
return 1;
}
return 0;
}
```
其中,`your_dsn_name` 是你的 DSN 名称,`your_table` 是你想要读取数据的表名,`your_column` 是你想要读取的列名。