Linux+mono+Apache访问SQLServer数据库和Oracle数据库
这几天研究Linux平台下,如果通过桌面程序和ASP.net访问SqlServer数据库和Oracle数据库,发现网上写的好多文章都不完善,很多是无用的多余的设置,而又不讲原理,本人研究了几日,经证实如下设置完全能满足要求。 前提:OS:CentOS5.5 A.安装mono2.6 增加一个
这几天研究Linux平台下,如果通过桌面程序和ASP.net访问SqlServer数据库和Oracle数据库,发现网上写的好多文章都不完善,很多是无用的多余的设置,而又不讲原理,本人研究了几日,经证实如下设置完全能满足要求。
前提:OS:CentOS5.5
A.安装mono2.6
增加一个新的代理文件,写入http://ftp.novell.com/pub/mono/download-stable/RHEL_5/。这是mono官方提供的针对CentOS的最新的mono平台。
yum --disablerepo=extras *mono* (注意:/etc/yum.repos.d/CentOS-Base.repo中的[extras]节也有一个版本的mono,不过很老了应该是1.1版本的,这里屏蔽掉了这个源,从而只安装最新版本的mono)
安装完后,会自动修改httpd相应的设置,无需手工再修改之。
B.安装freetds
yum install freetds
C.安装OracelInstantClient
去官方下载.zip压缩文件即可,就几十MB大小,解压到/opt/oracle/。
0. Linux下访问Oracle和SqlServer的小工具
0.1命令行访问SqlServer
安装完freetds后,会安装一个名为tsql的小工具,基本使用方法:
#tsql -S SqlServer服务器IP地址 -p 端口号 -U 用户名 -P 密码
正确登陆后,出现如下提示符:
>
输入
>select * from 表名;回车
>go;回车
然后会显示查询的内容。
0.2命令行访问Oracle
安装好InstantClient后,再下载一个sqlplus(很小)解压都同一目录下即可。把这一目录加入PATH
然后执行如下命令连接远程Oracle数据库
#sqlplus 用户名/密码@服务器IP/实例名
成功登陆后显示
SQL >
输入
SQL> select * from 表名;回车
即可看到查询结果。
1. Linux下mono访问SqlServer数据库
调用方式:
mono在调用方式上与.net Framework完全相同,都是采用System.Data.SqlClient命名空间中的类来完成数据库操作。
实现原理:
System.Data.SqlClient中的类库的实现靠的是通过P/Invoke调用freetds共享库的函数完成。所以必须安装freetds,在CentOS5.5中通过yum install freetds完成。
示例代码:
sqlserver.cs
编译命令:gmcs -r:System.Data sqlserver.cs
运行命令:mono sqlserver.exe
2.Linux下mono访问Oracle数据库
调用方式:
mono在调用方式上与.net Framework完全相同,都是采用System.Data.OracleClient命名空间中的类来完成数据库操作。
需要注意的是,目前微软已经不建议在.net 平台上用System.Data.OracleClient了,因为Oracle自己提供了一个Oracle.Data.Access类库来提供.net对Oracle数据库的访问。然而在mono平台上,System.Data.OracleClient仍是最好的访问Oracle数据库的方式。
实现原理:
System.Data.OracleClient中的类库的实现靠的是通过P/Invoke调用libclntsh.so共享库的函数完成。需要安装Oracle提供的InstantClient,这个软件是典型的绿色软件,下载后解压即可。需要注意的是要让mono能找到这个共享库,因此需要把它所在的路径加入LD_LIBRARY_PATH环境变量。例如解压后目录是/opt/oracle/instantclient_11_2,那么需要在/etc/profile中,写入如下:
export LD_LIBRARY_PATH=/opt/oracle/intantclient_11_2:$LD_LIBRARY_PATH,另外还需要设置NLS_LANG如下:
export NLS_LANG=AMERICAN_AMERICA.UTF8
最后做个软连接
ln -s /opt/oracle/instantclient_11_2/libclntsh.so.0.0.0 /opt/oracle/intantclient_11_2/libclntsh.so
然后执行ldconfig刷新缓存。
此时mono已经能够访问oracle数据库了。
示例代码:
oracle.cs
编译指令:gmcs -r:System.Data.OracleClient oracle.cs
运行指令:mono oracle.exe
3.基于Apache的mono对SqlServer的访问
在CentOS下,如果通过yum安装的freetds和mono以及mod_mono,那么没有什么问题了,与1基本相同。不再累述。
4.基于Apache的mono对Oracle的访问
这里需要注意,在/etc/profile设定的LD_LIBRARY_PATH和NLS_LANG等环境变量,在httpd里并不知情,httpd是服务程序,不是从shell启动的。此时/opt/novell/mono/bin/mod-mono-server也是通过httpd启动的,所以也不知道这些环境变量,这样导致了mono无法确定需要通过p/Invoke调用的libclntsh.so,所以会导致无法访问Oracle.解决方式:
修改/opt/novell/mono/bin/mod-mono-server,修改后如下:
#!/bin/sh
export LD_LIBRARY_PATH=/opt/oracle/instantclient_11_2:$LD_LIBRARAY_PATH
export NLS_LANG=AMERICAN_AMERICA.UTF8
exec /opt/novell/mono/bin/mono $.................................
然后重新启动httpd服务即可。
如果需要在JDBC下访问Oracle和SqlServer,参见我的另一篇博客:http://blog.csdn.net/smstong/article/details/8129536

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The import steps are as follows: Copy the MDF file to SQL Server's data directory (usually C:\Program Files\Microsoft SQL Server\MSSQL\DATA). In SQL Server Management Studio (SSMS), open the database and select Attach. Click the Add button and select the MDF file. Confirm the database name and click the OK button.

For objects with the same name that already exist in the SQL Server database, the following steps need to be taken: Confirm the object type (table, view, stored procedure). IF NOT EXISTS can be used to skip creation if the object is empty. If the object has data, use a different name or modify the structure. Use DROP to delete existing objects (use caution, backup recommended). Check for schema changes to make sure there are no references to deleted or renamed objects.

When the SQL Server service fails to start, here are some steps to resolve: Check the error log to determine the root cause. Make sure the service account has permission to start the service. Check whether dependency services are running. Disable antivirus software. Repair SQL Server installation. If the repair does not work, reinstall SQL Server.

To view the SQL Server port number: Open SSMS and connect to the server. Find the server name in Object Explorer, right-click it and select Properties. In the Connection tab, view the TCP Port field.

If you accidentally delete a SQL Server database, you can take the following steps to recover: stop database activity; back up log files; check database logs; recovery options: restore from backup; restore from transaction log; use DBCC CHECKDB; use third-party tools. Please back up your database regularly and enable transaction logging to prevent data loss.

In iOS17, Apple has more control over what apps can see in photos. Read on to learn how to manage app access by app. In iOS, Apple's in-app photo picker lets you share specific photos with the app, while the rest of your photo library remains private. Apps must request access to your entire photo library, and you can choose to grant the following access to apps: Restricted Access – Apps can only see images that you can select, which you can do at any time in the app or by going to Settings > ;Privacy & Security>Photos to view selected images. Full access – App can view photos

SQL Server database files are usually stored in the following default location: Windows: C:\Program Files\Microsoft SQL Server\MSSQL\DATALinux: /var/opt/mssql/data The database file location can be customized by modifying the database file path setting.

If the SQL Server installation fails, you can clean it up by following these steps: Uninstall SQL Server Delete registry keys Delete files and folders Restart the computer
