数据库基本链接语句

时间:2023-02-16 13:58:53

1、sqlserver数据库链接:

首先如果配置Web.config文件时,可能配置相应的文件如下:

<appSettings>
  <add key="dns" value="Data Source=feng; Initial Catalog=jxgl;User ID=sa;Connect Timeout = 60; " />
  <add key="datasource" value="feng" />
  <add key="database" value="jxgl" />
  <add key="userid" value="sa" />
  <add key="pwd" value="" />
  <add key="Copyright" value="(C)Copyright 2003,XXX软件有限责任公司" />
 </appSettings>

然后在程序中链接的语句设置如下:

MyConnString = System.Configuration.ConfigurationSettings.AppSettings["dns"];
Myconn = new SqlConnection(MyConnString);
Myconn.Open();
cmd.Connection = conf.Myconn;
cmd.CommandText=sql;
object sum =cmd.ExecuteScalar();
Myconn.Close();

(2)

"Data Source=Aron1;Initial Catalog=pubs;User Id=sa;Password=asdasd;"
   - or -
"Server=Aron1;Database=pubs;User ID=sa;Password=asdasd;Trusted_Connection=False"
   (both connection strings produces the same result)

using System.Data.SqlClient;
SqlConnection oSQLConn = new SqlConnection();
oSQLConn.ConnectionString="my connection string";
oSQLConn.Open();

2、oracle数据库链接

Web.config文件中配置如下:

<appSettings>
  <add key="dns" value="Data Source=ensure;User Id=ensure;Password=ensure;Integrated Security=no;" />
  <add key="Copyright" value="(C)Copyright 2005,郑州市云顶软件有限责任公司" />
 </appSettings>

程序中文件链接代码:

connString=ConfigurationSettings.AppSettings["dns"];
OracleConnection conn =new OracleConnection ();
    try
    {
        conn.Open();
        // Insert code to process data.
    }
        catch (Exception ex)
    {
        MessageBox.Show("Failed to connect to data source");
    } 
        conn.Close();

3、dbf类型数据库的链接:

Web.config中文件配置如下:

  <add key="DSN" value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d://dbf//2003.xls;Extended Properties=dBASE IV;User ID=Admin;Password=" />
  <add key="Copyright" value="(C)Copyright 2003, "/>
程序中如下:

public OleDbConnection Myconn;
public string MyConnString;
OleDbCommand mycomm;
MyConnString ="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d://dbf;Extended Properties=dBASE IV;User ID=Admin;Password=";
Myconn = new OleDbConnection(MyConnString);   
Myconn.Open();
mycomm=new OleDbCommand(sql,this.Myconn);
object sum =mycomm.ExecuteScalar();
this.close();

其它的链接方法可参考如下:

http://www.connectionstrings.com/index.asp

http://www.crazyit.cn/html/2005-01/1202.html