利用ODBC数据源连接数据库实现基础查询

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/peerless1024/article/details/62051089
#include<windows.h>
#include<stdio.h>
#include<stdlib.h>
#include<string>
#include<iostream>
#include "sql.h"
#include "sqltypes.h"
#include "sqlext.h"
using namespace std;


RETCODE retcode;  //结果返回集
SQLHDBC hdbc;  //定义连接句柄


void print();


int main(){


SQLHANDLE henv;  //定义环境句柄
unsigned char server[] = "NetMusicShop";   //数据源名称
unsigned char username[] = "sa";  //用户名
unsigned char password[] = "";  //密码


//连接数据库
retcode = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);//分配ODBC环境
retcode = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0);  //设置环境属性
retcode = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc);  //分配连接句柄
retcode = SQLConnect(hdbc, server, SQL_NTS, username, SQL_NTS,password, SQL_NTS);  //连接


if(!(retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)){
printf("数据库连接失败!");
}


else{
print();

}

SQLFreeConnect(hdbc);  //释放连接句柄
SQLFreeEnv(henv);  //释放ODBC环境句柄
printf("操作已完成!\n");
system("pause");
return 0;
}


void print()
{
unsigned char yuju[] = "select* from Users";
SQLHSTMT hstmt;  //定义语句句柄
int i =1;


char L1[20], L2[20], L3[20], L4[20], L5[20];
long lenOut1, lenOut2, lenOut3, lenOut4, lenOut5;


retcode = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);
if (retcode == SQL_SUCCESS){
retcode = SQLExecDirect(hstmt, yuju, SQL_NTS);
if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO){
retcode = SQLBindCol(hstmt, 1, SQL_C_CHAR, L1, sizeof(L1), &lenOut1);
retcode = SQLBindCol(hstmt, 2, SQL_C_CHAR, L2, sizeof(L2), &lenOut2);
retcode = SQLBindCol(hstmt, 3, SQL_C_CHAR, L3, sizeof(L3), &lenOut3);
retcode = SQLBindCol(hstmt, 4, SQL_C_CHAR, L4, sizeof(L4), &lenOut4);
retcode = SQLBindCol(hstmt, 5, SQL_C_CHAR, L5, sizeof(L5), &lenOut5);  //把所有捆绑过的数据字段的数据拷贝到相应的缓冲区


retcode = SQLFetch(hstmt);  //该函数用于将记录集的下一行变成当前行,并把所有捆绑过的数据字段的数据拷贝到相应的缓冲区


while (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO){
printf("%d\t%s\t%s\t%s\t%s\t%s\n", i, L1, L2,L3,L4,L5);
retcode = SQLFetch(hstmt);
i++;
}
}
}
SQLFreeStmt(hstmt, SQL_DROP);
}
展开阅读全文

为什么通过ODBC数据源连接数据库会出现问题??

06-12

用sql server数据库时用JDBC-ODBC连接数据库时设置数据源然后连接数据库出现如下错误:(程序绝对没得问题)rnjavax.servlet.ServletException: [Microsoft][ODBC ???????] ???????????????????rn org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:867)rn org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:800)rn org.apache.jsp.testsqlserver2_jsp._jspService(testsqlserver2_jsp.java:108)rn org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:133)rn javax.servlet.http.HttpServlet.service(HttpServlet.java:853)rn org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:311)rn org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)rn org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)rn javax.servlet.http.HttpServlet.service(HttpServlet.java:853)rnrnrnroot cause rnrnjava.sql.SQLException: [Microsoft][ODBC ???????] ???????????????????rn sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6958)rn sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7115)rn sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(JdbcOdbc.java:3074)rn sun.jdbc.odbc.JdbcOdbcConnection.initialize(JdbcOdbcConnection.java:323)rn sun.jdbc.odbc.JdbcOdbcDriver.connect(JdbcOdbcDriver.java:174)rn java.sql.DriverManager.getConnection(DriverManager.java:512)rn java.sql.DriverManager.getConnection(DriverManager.java:171)rn org.apache.jsp.testsqlserver2_jsp._jspService(testsqlserver2_jsp.java:59)rn org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:133)rn javax.servlet.http.HttpServlet.service(HttpServlet.java:853)rn org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:311)rn org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)rn org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)rn javax.servlet.http.HttpServlet.service(HttpServlet.java:853)rnrn用access时设数据源时也会出现这样的问题rn以前用这种方法连数据库时没问题,好像是突然出现这种情况,怎么回事??rnrn附连接数据库的方法:rnjava.sql.Connection sqlConn; rnjava.sql.Statement stmt; rnjava.sql.ResultSet rs; rnClass.forName("sun.jdbc.odbc.JdbcOdbcDriver");rnsqlConn=java.sql.DriverManager.getConnection("jdbc:odbc:book","",""); rnstmt=sqlConn.createStatement(); rnrn 论坛

利用VB.NET来读取ODBC数据源

10-03

这段文章也许对大家有帮助。。。董vb 的可以翻译bcbrnrnrnrn在论坛中有人提出关于如何在.NET中读取ODBC数据源名的问题,不幸的是,在.NET Framework的类库中没有提供任何类或方法来实现它。然而,这有个简单的方法可以为它得到答案。rnrn  事实上,所有的ODBC数据源名都存放在Windows系统的注册表中。如果你知道注册表中正确的项值,你就可以通过.NET Framework类库提供的注册表类来读取该项值下的所有DSN列表。rnrn  所有ODBC数据源名都存放在Windows注册表下的:LocalMachine\ODBC\ODBC.INI\ODBC Data Sources(系统DSN)和CurrentUser\Software\ODBC\ODBC.INI\ODBC Data Sources(用户DSN) 键值中。 rnrn  Imports Microsoft.Win32 '引用名字空间 rnrn  下面的源代码是演示读取ODBC DSN列表内容并加入到ListBox控件中。来测试这段代码,建立一个Windows 应用程式,添加一个ListBox控件到窗体表单中,并将ReadODBCDSNs方法加到程式代码中。然后,可用一个命令按钮的单或双击事件或用窗体的导入事件来引用这个方法。 rnrnPrivate Sub ReadODBCSNs()rnrnDim str As StringrnDim rootKey As RegistryKey, subKey As RegistryKeyrnDim dsnList() As StringrnrootKey = Registry.LocalMachinernstr = "SOFTWARE\\ODBC\\ODBC.INI\\ODBC Data Sources"rnsubKey = rootKey.OpenSubKey(str)rndsnList = subKey.GetvalueNames()rnListBox1.Items.Add("System DSNs")rnListBox1.Items.Add("================")rnDim dsnName As StringrnrnFor Each dsnName In dsnListrnListBox1.Items.Add(dsnName)rnNextrnsubKey.Close()rnrnrootKey.Close()rnrn'Load User DSNsrnrootKey = Registry.CurrentUserrnstr = "SOFTWARE\\ODBC\\ODBC.INI\\ODBC Data Sources"rnsubKey = rootKey.OpenSubKey(str)rndsnList = subKey.GetvalueNames()rnrnListBox1.Items.Add("================")rnListBox1.Items.Add("UserDSNs")rnListBox1.Items.Add("================")rnrnFor Each dsnName In dsnListrnListBox1.Items.Add(dsnName)rnNextrnsubKey.Close()rnrnrootKey.Close()rnEnd Sub rn 论坛

没有更多推荐了,返回首页