各种数据库连接串,IIS下用ODBC连接数据库

作者:计算机知识

$connection = int odbc_connect(string dsn, string user, string password)创立数据库连接,

2012-12-13 22:27 (分类:暗中认可分类)

1.率先创建数据源,不荒谬情状下载调控面板-管理工科具-数据源,展开后有客户DSN系统DSN
二者分别在于系统级的DSN,就是对该系统的装有登入客户可用,客户DSN是只对创设它的客商可用
2.安顿自个儿的DSN,比方连接ACCESS数据库
单击“增加”开关,展现“创造新数据源”对话框。选拔“Microsoft Access Driver(*.mdb)”,
单击<实现>,步向下一步。
随起个数据源名称(如:MyData),再单击“选择(S)...”,采取Access数据库,分明,就OK了。

本人这里再三再四的是ACCESS 数据源名称是 “登入框“  里面独有一个表 登陆表 七个字段 ID 客户名 密码

非常表明,win7 63人的同班展开数据源,单击“增多”按键后,是否绝非驱动,去这里
C:WindowsSysWOW64odbcad32.exe    三十二人数据源

如此那般数据源就加多工作有成了

树立程序  本次通过vc6.0 创立MFC exe 程序
最主要选取MFC封装的 odbc类 

选料单文书档案,必要包涵数据库(查看数据库使用文件扶助),在红尘点击数据源,选用刚刚创设的数据源,
和呼应的表,基本就这么

做的是个登入框主页面验证登入,注册开关蹦出个对话框为注册页面;
上边是至关重大,弄了某个天才清楚,在单文书档案主页面如下图片 1

八个编辑框增加成员变量为m_name,m_pwd;为记名开关增加新闻相应函数 内容如下

UpdateData(TRUE);//刷新控件的值到对应的变量 
if(m_name==m_各种数据库连接串,IIS下用ODBC连接数据库。pSet->m_column1&&m_pwd==m_pSet->m_column2)
//与数据库举行比对m_column1为数据库 客户名字段 m_column2为数据库 密码字段 MessageBox("OK");//成功弹出ok
else
MessageBox("E奥迪Q5RO翼虎");//失利弹出error 这里说一下m_pSet 这几个是CRecordset类中的指针 第贰个必要为点击注册按键弹出挂号页面,这里自个儿新建两个对话框 ,增多二个dialog,如下

图片 2

为七个编辑框设置成员变量为 m_name_zhuce 和 m_pwd_zhuce,
那些对话框要承继哪个类呢? Crecordview?Crecordset?照旧 CDialog?

1.先是种状态:为了能使用m_pSet 对话框承袭 Crecordview类也许Crecordset类,难题出来了在主窗口的登记开关里的代码 符合规律为新建贰个对话框叫SecondDialog,它的类名字为SecondDialog。
双击主对话框上的注册开关步向代码编辑区。
NO.1把SecondDialog.cpp的#include "SecondDialog.h" 参与到主对话框"类"中,
要在#include "stdafx.h"后 
NO.2、在按键事件之中写上边两句代码CSecondDialog dlg; dlg.DoModal();
这么就足以在点击 注册开关后 弹出挂号对话框了  
然则在Crecordview类也许Crecordset类 里未有DoModal()那几个函数 
于是根据地点的代码,无法编写翻译通过 
咋办吧?是否因此另外方法吗?showWindows()那个函数是或不是能把它显得出来呢? 
2.次之种景况:依然再而三CDialog类,那样能够应用CSecondDialog dlg; 
dlg.DoModal();了
什么让那么些那么些对话框连接数据库呢?这里就用上 MFC 封装的odbc类了,
只顾上边的代码了,每一个自个儿都加注释了,很累滴 UpdateData(TRUE); //更新控件音讯
CDatabase base1; // 创建CDatabase类 对象
//通过对象去老是数据库 
// 数据源名称,独占展开?只读张开?数据源信息,加载ODBC光标动态连接库 ? base1.Open(NULL,FALSE,FALSE,"ODBC;DSN=登录框;UID=NULL;PWD=NULL",FALSE);
CRecordset object(&base1); //构造记录集对象object(关联记录集)
//这里要动用具体的类,小编建的工程里 是CStuSet object(&base1)CStuSet承接CRecordset
//记录集对象object 以动态格局展开student表全体字段,获取数据库数据
object.Open(CRecordset::dynaset,“Select * from students”);

//进行询问操作 ,查询刚才关联的表
object.m_strFilter="name='admin'"  //m_strFilter约等于where 查询姓名称叫admin的数量
object.m_strSort="name" //m_strSort相当于order by 依据name 排序
object.Requery();  //重新创设记录集
// void MoveNext( )前进三个笔录; void MovePrev( )后退八个记下; 
//void MoveFirst( )滚动到记录集中的第一个记录;  
//void MoveLast( )滚动到记录聚焦的末段四个记录//进行增加操作
object.AddNew();  //在表尾进行增加操作
object.m_column1="admin"; //为字段1赋值
object.m_column2="123"; //为字段2赋值
object.Update(); //新记录存入数据库
object.Requrey(); //重新创设记录集 //实行删除操作
//调用Delete()成员函数能够达成记录删除,在调用Delete()函数后不需调用Update()函数,
 object.Delete();  
if (!object.IsEOF())   //剖断光标是不是在记录集最终
object.MoveNext();  //不是则向下移一个人
else  
object.MoveLast(); //是的化滚动到最后贰个记下//写到这里很累了,要下课了,前天后续吧

很倒霉,写完未保存,那是首次了 烦, 3.第二种处境 
如故利用CDialog类 来做登记对话框,继续选择数据源,不用代码连接数据库 CLoginView *p=new CLoginView; //那个CLoginView承接CRecordView类,创制工程时建好
// 实例化类的靶子 为指针 p
p->m_pSet = new CLoginSet;
p->m_pSet-> Open();
p->m_pSet->AddNew();
//对记录集中的字段赋值
p->m_pSet->m_column1 = "wocao";
p->m_pSet->m_column2 = "123";
//更新记录集,将新记录存入数据源
p->m_pSet->Update();
//刷新记录集,并使记录集指针回到第一条记下
p->m_pSet->Requery();在编写翻译时会报错,基类中class CLoginSet : public CRecordset 正是以此类有一对是个体的
出于无奈使用 ,作者一贯改成国有的,ok了  4.第三种状态
更相对些,直接建设构造对话框mfc,不带有数据源,全部用代码完毕
对话框 分明按键 音信响应函数 如下 CDatabase base1; // 创制CDatabase类 对象
//通过对象去老是数据库 
// 数据源名称,独占张开?只读打开?数据源消息,加载ODBC光标动态连接库 ? 
base1.Open(NULL,FALSE,FALSE,"ODBC;DSN=登陆框",FALSE);
CRecordset object(&base1); //构造记录集对象object(关联记录集)
//小编那边运用的是CStuSet object(&base1); 是继续CRecordset类
//记录集对象object 以动态格局展开student表全体字段,获取数据库数据
object.Open(CRecordset::dynaset,"Select * from 登录表");
CString sql="INSERT INTO 登录表(用户名,密码) VALUES('laji2','123')";

base1.ExecuteSQL(sql);那样简单多了吧,ExecuteSQL那几个函数正是用来实践SQL代码的,SQL语句不会就本身查吧,要稳重的是 access 和 另外数据库 语法只怕有个别小分歧,自身只顾啊,
30日了,odbc就到那边吧     

 

Overview

Generally, one of the first steps when you are trying to work with databases is open it. You can find several types of those, and each have a different mode of connection. When you try to connect with your database sometimes, you don't know the correct connection string that you must use. It is for that I wrote this article. I wanted to compile the connection strings to the majority of known databases...

Overview

Generally, one of the first steps when you are trying to work with databases is open it. You can find several types of those, and each have a different mode ofconnection. When you try to connect with your database sometimes, you don't know the correctconnectionstringthat you must use. It is for that I wrote this article. I wanted to compile theconnectionstrings to the majority of known databases...

$query_string = "查询记录的准则"
如:$query_string = "select * from table"

ODBC DSN Less Connection

ODBC DSN LessConnection

用$cur = int odbc_exec(int connection_id, string query_string)检索数据库,将记录集放入$cur变量中。

ODBC Driver for dBASE

strConnection = _T("Driver={Microsoft dBASE Driver (*.dbf)};DriverID=277;"
                   "Dbq=c:\DatabasePath;");

Note: You must specify the filename in the SQL statement... For example:

CString strQuery = _T("Select Name, Address From Clients.dbf");

ODBC Driver for dBASE

strConnection = _T("Driver={Microsoft dBASE Driver };DriverID=277;"                   "Dbq=c:\DatabasePath;");

Note:You must specify the filename in the SQL statement... For example:

CString strQuery = _T("Select Name, Address From Clients.dbf");

再用while (odbc_fetch_row($cur)){
$var1=odbc_result($cur,1);
$var2=odbc_result($cur,1);
...
}
读取odbc_exec()再次来到的多寡集$cur。

ODBC Driver for Excel

strConnection = _T("Driver={Microsoft Excel Driver (*.xls)};DriverId=790;"
    bq=C:\DatabasePath\DBSpreadSheet.xls;DefaultDir=c:\databasepath;");

ODBC Driver for Excel

strConnection = _T("Driver={Microsoft Excel Driver };DriverId=790;"    bq=C:\DatabasePath\DBSpreadSheet.xls;DefaultDir=c:\databasepath;");

最后是odbc_close(int connection_id)关闭数据库的接连。

ODBC Driver for Text

strConnection = _T("Driver={Microsoft Text Driver (*.txt; *.csv)};"
        "Dbq=C:\DatabasePath\;Extensions=asc,csv,tab,txt;");

If you are using tab delimited files, you must create the schema.ini file, and you must inform theFormat=TabDelimited option in your connection string.

Note: You must specify the filename in the SQL statement... For example:

CString strQuery = _T("Select Name, Address From Clients.csv");

ODBC Driver for Text

strConnection = _T("Driver={Microsoft Text Driver (*.txt; *.csv)};"        "Dbq=C:\DatabasePath\;Extensions=asc,csv,tab,txt;");

If you are using tab delimited files, you must create theschema.inifile, and you must inform theFormat=TabDelimitedoption in yourconnectionstring.

Note:You must specify the filename in the SQL statement... For example:

CString strQuery = _T("Select Name, Address From Clients.csv");

odbc_result()函数是取当前记录的钦定字段值。  

Visual FoxPro

If you are using a database Container, the connection string is the following:

strConnection = _T("Driver={Microsoft Visual Foxpro Driver};UID=;"
    ourceType=DBC;SourceDB=C:\DatabasePath\MyDatabase.dbc;Exclusive=No");

If you are working without a database container, you must change the SourceType parameter by DBF as in the following connection string:

strConnection = _T("Driver={Microsoft Visual Foxpro Driver};UID=;"
    "SourceType=DBF;SourceDB=C:\DatabasePath\MyDatabase.dbc;Exclusive=No");

Visual FoxPro

If you are using adatabaseContainer, theconnectionstringis the following:

strConnection = _T("Driver={Microsoft Visual Foxpro Driver};UID=;"    ourceType=DBC;SourceDB=C:\DatabasePath\MyDatabase.dbc;Exclusive=No");

If you are working without adatabase container, you must change theSourceTypeparameter byDBFas in the followingconnectionstring:

strConnection = _T("Driver={Microsoft Visual Foxpro Driver};UID=;"    "SourceType=DBF;SourceDB=C:\DatabasePath\MyDatabase.dbc;Exclusive=No");

  

ODBC Driver for Access

strConnection = _T("Driver={Microsoft Access Driver (*.mdb)};"
        "Dbq=c:\DatabasePath\dbaccess.mdb;Uid=;Pwd=;");

If you are using a Workgroup (System database): you need to inform the SystemDB Path, the User Name and itspassword. For that, you have two solutions: inform the user and password in the connection string or in the moment of the open operation. For example:

strConnection = _T("Driver={Microsoft Access Driver (*.mdb)};"
        "Dbq=C:\VC Projects\ADO\Samples\AdoTest\dbTestSecurity.mdb;"
        "SystemDB=C:\Program Files\Microsoft Office\Office\SYSTEM.mdw;"
        "Uid=Carlos Antollini;Pwd=carlos");

or may be:

strConnection = _T("Driver={Microsoft Access Driver (*.mdb)};"
        "Dbq=C:\VC Projects\ADO\Samples\AdoTest\dbTestSecurity.mdb;"
        "SystemDB=C:\Program Files\Microsoft Office\Office\SYSTEM.mdw;");
if(pDB.Open(strConnection, "DatabaseUser", "DatabasePass"))
{
    DoSomething();
    pDB.Close();
}

If you want to open in Exclusive mode:

strConnection = _T("Driver={Microsoft Access Driver (*.mdb)};"
        "Dbq=c:\DatabasePathdbaccess.mdb;Exclusive=1;");

ODBC Driver for Access

strConnection = _T("Driver={Microsoft Access Driver };"        "Dbq=c:\DatabasePath\dbaccess.mdb;Uid=;Pwd=;");

If you are using a Workgroup (System database): you need to inform theSystemDB Path, theUser Nameand itspassword. For that, you have two solutions: inform theuserandpasswordin theconnectionstringor in the moment of theopenoperation. For example:

strConnection = _T("Driver={Microsoft Access Driver };"        "Dbq=C:\VC Projects\ADO\Samples\AdoTest\dbTestSecurity.mdb;"        "SystemDB=C:\Program Files\Microsoft Office\Office\SYSTEM.mdw;"        "Uid=Carlos Antollini;Pwd=carlos");

or may be:

strConnection = _T("Driver={Microsoft Access Driver };"        "Dbq=C:\VC Projects\ADO\Samples\AdoTest\dbTestSecurity.mdb;"        "SystemDB=C:\Program Files\Microsoft Office\Office\SYSTEM.mdw;");if(pDB.Open(strConnection, "DatabaseUser", "DatabasePass")){    DoSomething();    pDB.Close();}

If you want to open in Exclusive mode:

strConnection = _T("Driver={Microsoft Access Driver };"        "Dbq=c:\DatabasePathdbaccess.mdb;Exclusive=1;");

ODBC Driver for SQL Server

For Standard security:

strConnection = _T("Driver={SQL Server};Server=MyServerName;"
        "Trusted_Connection=no;"
        "Database=MyDatabaseName;Uid=MyUserName;Pwd=MyPassword;");

For Trusted Connection security (Microsoft Windows NT integrated security):

strConnection = _T("Driver={SQL Server};Server=MyServerName;"
     "Database=myDatabaseName;Uid=;Pwd=;");

Also, you can use the parameter Trusted_Connection that indicates that you are using the Microsoft Windows NT Authentication Mode to authorize user access to the SQL Server database. For example:

strConnection = _T("Driver={SQL Server};Server=MyServerName;"
    "Database=MyDatabaseName;Trusted_Connection=yes;");

If the SQL Server is running in the same computer, you can replace the name of the server by the word (local) like in the following sample:

strConnection = _T("Driver={SQL Server};Server=(local);"
        "Database=MyDatabaseName;Uid=MyUserName;Pwd=MyPassword;");

If you want to connect with a remote SQL Server, you must inform the address, the port, and the Network Library to use:

The Address parameter must be an IP address and must include the port. The Network parameter can be one of the following:

  • dbnmpntw Win32 Named Pipes
  • dbmssocn Win32 Winsock TCP/IP
  • dbmsspxn Win32 SPX/IPX
  • dbmsvinn Win32 Banyan Vines
  • dbmsrpcn Win32 Multi-Protocol (Windows RPC)

For more information, see Q238949.

strConnection = _T("Driver={SQL Server};Server=130.120.110.001;"
     "Address=130.120.110.001,1052;Network=dbmssocn;Database=MyDatabaseName;"
     "Uid=myUsername;Pwd=myPassword;");

ODBC Driver for SQL Server

For Standard security:

strConnection = _T("Driver={SQL Server};Server=MyServerName;"        "Trusted_Connection=no;"        "Database=MyDatabaseName;Uid=MyUserName;Pwd=MyPassword;");

For TrustedConnectionsecurity (Microsoft Windows NT integrated security):

strConnection = _T("Driver={SQL Server};Server=MyServerName;"     "Database=myDatabaseName;Uid=;Pwd=;");

Also, you can use the parameterTrusted_Connectionthat indicates that you are using the Microsoft Windows NT Authentication Mode to authorize user access to the SQL Server database. For example:

strConnection = _T("Driver={SQL Server};Server=MyServerName;"    "Database=MyDatabaseName;Trusted_Connection=yes;");

If the SQL Server is running in the same computer, you can replace the name of the server by the word``like in the following sample:

strConnection = _T("Driver={SQL Server};Server=;"        "Database=MyDatabaseName;Uid=MyUserName;Pwd=MyPassword;");

If you want to connect with a remote SQL Server, you must inform the address, the port, and the Network Library to use:

TheAddressparameter must be an IP address and must include the port. TheNetworkparameter can be one of the following:

  • dbnmpntwWin32 Named Pipes
  • dbmssocnWin32 Winsock TCP/IP
  • dbmsspxnWin32 SPX/IPX
  • dbmsvinnWin32 Banyan Vines
  • dbmsrpcnWin32 Multi-Protocol (Windows RPC)

For more information, seeQ238949.

strConnection = _T("Driver={SQL Server};Server=130.120.110.001;"     "Address=130.120.110.001,1052;Network=dbmssocn;Database=MyDatabaseName;"     "Uid=myUsername;Pwd=myPassword;");

ODBC Driver for Oracle

For the current Oracle ODBC driver from Microsoft:

strConnect = _T("Driver={Microsoft ODBC for Oracle};Server=OracleServer.world;"
        "Uid=MyUsername;Pwd=MyPassword;");

For the older oracle ODBC driver from Microsoft:

strConnect = _T("Driver={Microsoft ODBC Driver for Oracle};"
     "ConnectString=OracleServer.world;Uid=myUsername;Pwd=myPassword;");

ODBC Driver forOracle

For the currentOracleODBC driver from Microsoft:

strConnect = _T("Driver={Microsoft ODBC for Oracle};Server=OracleServer.world;"        "Uid=MyUsername;Pwd=MyPassword;");

For the olderoracleODBC driver from Microsoft:

strConnect = _T("Driver={Microsoft ODBC Driver for Oracle};"     "ConnectString=OracleServer.world;Uid=myUsername;Pwd=myPassword;");

本文由bwin必赢发布,转载请注明来源

关键词: ODBC MySQL SqlServer oracle 3 database