首页 > 开发 > 综合 > 正文

数据库连接字在Web.config里的用法

2024-07-21 02:24:28
字体:
来源:转载
供稿:网友


在asp.net中的web程序的设置中我们必须用到web.config来存储数据库连接字.事实上这是个

很好的做法,因为可以省去我们很多的麻烦还可以帮助我们避免不必要的错位,是的很多情况下

我就是这样做.它通过xml来记录这些信息.具体的是在<appsettings>....</appsettings>这个

标记中来记录的.这里请看一个oracle的例子如下:

<appsettings>
    <add key="oracleconnectionstring" value="provider=oraoledb.oracle.1;
persist security info=false;password=blah;user id=greg;data source=sph;" />
    <add key="sqlconnectionstring" value="data source=sql1;initial catalog=id_v;
integrated security=sspi;persist security info=false;workstation id=th03d374;
packet size=4096"/>  
<appsettings>

而在你的应用程序中你只要这样写,就可以了,如下:

string conn = configurationsettings.appsettings["oracleconnectionstring"];
oledbconnection myconnection = new oledbconnection(conn);

很轻松是吗?不必每次都输入同样的连接字,也不要记住那些讨厌的信息了,只需要起一个好记的

名字就可以.

好了下面我在来给出其他的一些连接字

mysql的连接字:
connectionstring = "data source=localhost;" +
                              "database=mysqldatabase;" +
                              "user id=myusername;" +
                              "password=mypassword;" +
                              "command logging=false";
ole db的:
ibm as/400 ole db 的

' vb.net
dim ooledbconnection as oledb.oledbconnection
dim sconnstring as string = _
           "provider=ibmda400.datasource.1;" & _
           "data source=myas400dbname;" & _
           "user id=myusername;" & _
           "password=mypassword"
ooledbconnection = new oledb.oledbconnection(sconnstring)
ooledbconnection.open()
 
jet ole db 的

' vb.net
dim ooledbconnection as oledb.oledbconnection
dim sconnstring as string = _
         "provider=microsoft.jet.oledb.4.0;" & _
         "data source=c:/mypath/myjet.mdb;" & _
         "user id=admin;" & _
         "password="
ooledbconnection = new oledb.oledbconnection(sconnstring)
ooledbconnection.open()
 
oracle ole db 的

' vb.net
dim ooledbconnection as oledb.oledbconnection
dim sconnstring as string = _
         "provider=oraoledb.oracle;" & _
         "data source=myoracledb;" & _
         "user id=myusername;" & _
         "password=mypassword"
ooledbconnection = new oledb.oledbconnection(sconnstring)
ooledbconnection.open()
 
sql server ole db 的

' vb.net
dim ooledbconnection as oledb.oledbconnection
dim sconnstring as string = _
         "provider=sqloledb;" & _
         "data source=myservername;" & _
         "initial catalog=mydatabasename;" & _
         "user id=myusername;" & _
         "password=mypassword"
ooledbconnection = new oledb.oledbconnection(sconnstring)
ooledbconnection.open()
 
sybase ase ole db 的

' vb.net
dim ooledbconnection as oledb.oledbconnection
dim sconnstring as string = _
         "provider=sybase ase ole db provider;" & _
         "data source=mydatasourcename;" & _
         "server name=myservername;" & _
         "database=mydatabasename;" & _
         "user id=myusername;" & _
         "password=mypassword"
ooledbconnection = new oledb.oledbconnection(sconnstring)
ooledbconnection.open()

sql server在system.data.sqlclient 

sqlconnection osqlconn = new sqlconnection();
osqlconn.connectionstring = "data source=(local);" +
                            "initial catalog=mysqlserverdbname;" +
                            "integrated security=sspi";
osqlconn.open();


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表