1、配置数据库连接字符串(Winform,asp.net2.0通用):
CS使用:
附: 简单调用VS自带的数据源选择对话框实现数据库连接字符串配置
引用:http://dev.csdn.net/author/hustsky/effef449c43a46188702915930ea648b.html
<?
xml version="1.0" encoding="utf-8"
?>
< configuration >
< connectionStrings > < add name ="sqlConn" connectionString ="Data Source=Ip;Initial Catalog=txcard;User ID=u;Password=p" providerName ="System.Data.SqlClient" /> < add name ="JetConn" connectionString ="Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:access.mdb; Persist Security Info=False;" providerName ="System.Data.OleDb" />
< add name ="ExcelConn" connectionString ="Dsn=Excel Files;dbq=data.xls;defaultdir=.; driverid=790;maxbuffersize=2048;pagetimeout=5" providerName ="System.Data.Odbc" />
</ connectionStrings >
</ configuration >
< configuration >
< connectionStrings > < add name ="sqlConn" connectionString ="Data Source=Ip;Initial Catalog=txcard;User ID=u;Password=p" providerName ="System.Data.SqlClient" /> < add name ="JetConn" connectionString ="Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:access.mdb; Persist Security Info=False;" providerName ="System.Data.OleDb" />
< add name ="ExcelConn" connectionString ="Dsn=Excel Files;dbq=data.xls;defaultdir=.; driverid=790;maxbuffersize=2048;pagetimeout=5" providerName ="System.Data.Odbc" />
</ connectionStrings >
</ configuration >
using
System.Configuration;
private string ConnectionString = ConfigurationManager.ConnectionStrings[ " sqlConn " ].ConnectionString;
private string ConnectionString = ConfigurationManager.ConnectionStrings[ " sqlConn " ].ConnectionString;
附: 简单调用VS自带的数据源选择对话框实现数据库连接字符串配置
我们经常在开发应用程序中提供择数据源和数据连接配置的接口,方便用户配置数据库连接字符串。典型的方法是将这些信息写在配置文件里,如web.config或app.config文件里.
但是客户并不是专业的编程人员,一不小心就会使连接串写错,导致系统无法运行,最好是让用户能够通过一个图形界面来选择数据源和数据连接的信息,如下图:
我们可以通过调用Visual Studio中的数据库连接配置UI来简单实现。
具体的实现方法如下:
首 先添加对Microsoft.Data.ConnectionUI.Dialog.dll的引用,这个assembly在VS2005的安装 目录下,C:/Program Files/Microsoft Visual Studio 8/Common7/IDE下边,我的VS安装在C盘,下面是代码:
using
System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Data.ConnectionUI;
namespace ConnectionBuilder
{
public partial class ConnectionForm : Form
{
public ConnectionForm()
{
InitializeComponent();
}
private void btnCreateConnection( object sender, EventArgs e)
{
DataConnectionDialog dialog = new DataConnectionDialog();
// 添加数据源列表,可以向窗口中添加自己程序所需要的数据源类型
dialog.DataSources.Add(DataSource.SqlDataSource);
dialog.DataSources.Add(DataSource.OdbcDataSource);
dialog.SelectedDataSource = DataSource.OdbcDataSource;
dialog.SelectedDataProvider = DataProvider.OdbcDataProvider;
// 只能够通过DataConnectionDialog类的静态方法Show出对话框
// 不同使用dialog.Show()或dialog.ShowDialog()来呈现对话框
if (DataConnectionDialog.Show(dialog, this ) == DialogResult.OK)
{
txtConnectionString.Text = dialog.ConnectionString;
}
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Data.ConnectionUI;
namespace ConnectionBuilder
{
public partial class ConnectionForm : Form
{
public ConnectionForm()
{
InitializeComponent();
}
private void btnCreateConnection( object sender, EventArgs e)
{
DataConnectionDialog dialog = new DataConnectionDialog();
// 添加数据源列表,可以向窗口中添加自己程序所需要的数据源类型
dialog.DataSources.Add(DataSource.SqlDataSource);
dialog.DataSources.Add(DataSource.OdbcDataSource);
dialog.SelectedDataSource = DataSource.OdbcDataSource;
dialog.SelectedDataProvider = DataProvider.OdbcDataProvider;
// 只能够通过DataConnectionDialog类的静态方法Show出对话框
// 不同使用dialog.Show()或dialog.ShowDialog()来呈现对话框
if (DataConnectionDialog.Show(dialog, this ) == DialogResult.OK)
{
txtConnectionString.Text = dialog.ConnectionString;
}
}
}
}
引用:http://dev.csdn.net/author/hustsky/effef449c43a46188702915930ea648b.html