ODBC Oracle API DB 2 API DB 2
ODBC基本概念 Oracle API 应用 程序 DB 2 API DB 2 SQL Server API SQL Server ODBC API 应用 程序 Oracle Access API Access
实验1 -操作步骤 o o 建立支持MFC大console项目vcado 在主程序文件中vcado. cpp增加如下指令 #import "c: Program FilesCommon FilesSystemADOmsado 15. dll" no_namespace rename("EOF", "End. Of. File") #include <stdio. h> #define TESTHR( _hr ) if FAILED(_hr) _com_issue_error(_hr); void Print. Output (_Recordset. Ptr& rs); void Excute. Sql();
实验1 -Excute. Sql() void Excute. Sql() { Co. Initialize(NULL); try { _Connection. Ptr cn("ADODB. Connection"); _Recordset. Ptr rs("ADODB. Recordset"); _bstr_t str. Missing(L""); _bstr_t str. Conn(L"Provider=sqloledb; Data Source=neuzhao 1; Initial Catalog=Northwind; User Id=sa; Password=sa; "); _bstr_t str. SQL(L"select * from products"); // 开发连接 cn->Open(str. Conn, str. Missing, L"", ad. Connect. Unspecified); rs->Open (str. SQL, _variant_t((IDispatch *) cn, true), ad. Open. Dynamic, ad. Lock. Unspecified, ad. Cmd. Text); //输出查询结果 Print. Output(rs); rs->Close(); cn->Close(); } catch (_com_error &e) { printf("Description = %sn", (char*) e. Description()); : : Co. Uninitialize(); } }
实验1 - Print. Output void Print. Output (_Recordset. Ptr& rs) { long i=0; _variant_t vt. Value; while(!rs->Get. End. Of. File()) { for(i=0; i<rs->Fields->Count ; i++) { vt. Value=rs->Get. Fields ()->Get. Item(i)->Value; printf("%5 s", (char *)_bstr_t(vt. Value)); } printf("n"); rs->Move. Next (); } }
实验1 -主函数 int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]) { int n. Ret. Code = 0; // initialize MFC and print and error on failure if (!Afx. Win. Init(: : Get. Module. Handle(NULL), NULL, : : Get. Command. Line(), 0)) { // TODO: change error code to suit your needs cerr << _T("Fatal Error: MFC initialization failed") << endl; n. Ret. Code = 1; } else { //调用Excute. Sql(); } } return n. Ret. Code;
实验2 -操作步骤 o o o 增加引用 编写子程序Public Sub Execute. X()执行查 询 编写子程序Public Sub Print. Output(rst. Temp As ADODB. Recordset) 输出记录集内容
实验2 - Public Sub Execute. X() Dim str. SQL As String '存储SQL语句 Dim str. Cnn As String '数据库链接字符串,其描述的了数据库驱动、数据库名称、帐 户及密码等信息 Dim cnn 1 As ADODB. Connection 'ADO 中Connection 对象 Dim rst. Result As ADODB. Recordset 'ADO 中记录集,代表查询之后的记录的集合 Dim err. Loop As ADODB. Error 'ADO 中错误对象 str. SQL = txt. Sql. Text '获得SQL命令 '构建链接字符串 str. Cnn = "Provider=sqloledb; " & _ "Data Source=neuzhao 1; Initial Catalog=Northwind; User Id=sa; Password=sa; " '出错处理 On Error Go. To Err_Execute Set cnn 1 = New ADODB. Connection '创建Connection 对象 '打开链接 cnn 1. Open str. Cnn '创建Recordset 对象 Set rst. Result = New ADODB. Recordset '执行SQL,并把查询结果保存到rst. Result. Open str. SQL, cnn 1, ad. Open. Dynamic, , ad. Cmd. Text
实验2 - Public Sub Execute. X() ' 打印查询结果(调试用) Debug. Print "查询结果" ' 打印查询结果(调试用) Print. Output rst. Result '关于对象,要先关闭记录集,之后再关闭Connection rst. Result. Close cnn 1. Close Exit Sub Err_Execute: ' 循环输出错误信息 If rst. Result. Active. Connection. Errors. Count >= 0 Then For Each err. Loop In rst. Titles. Active. Connection. Errors Msg. Box "Error number: " & err. Loop. Number & vb. Cr & _ err. Loop. Description Next err. Loop End If Resume Next End Sub
Public Sub Print. Output(rst. Temp As ADODB. Recordset) Dim str. Text As String ' 遍历Recordset. Do While Not rst. Temp. EOF '输出每个字段的属性 For i = 0 To rst. Temp. Fields. Count - 1 str. Text = str. Text & vb. Tab & rst. Temp. Fields(i). Value Next i str. Text = str. Text & vb. Cr. Lf rst. Temp. Move. Next Loop '将结果显示出来 txt. Result. Text = str. Text End Sub
实验2 - cmd. Query_Click() 为”查询”按扭增加事件处理程序 Private Sub cmd. Query_Click() Execute. X End Sub
- Slides: 20