数据库连接字符串
from:http://www.dofactory.com/Connect/Connect.aspx#_self15
Microsoft SqlServer 数据库连接字符串
Microsoft SQL Server // ODBC DSN using System.Data.Odbc; OdbcConnection conn = new OdbcConnection(); conn.ConnectionString = "Dsn=DsnName;" + "Uid=UserName;" + "Pwd=Secret;"; conn.Open(); // ODBC -- Standard Connection using System.Data.Odbc; OdbcConnection conn = new OdbcConnection(); conn.ConnectionString = "Driver={SQL Server};" + "Server=ServerName;" + "DataBase=DataBaseName;" + "Uid=UserName;" + "Pwd=Secret;"; conn.Open(); // ODBC -- Trusted Connection using System.Data.Odbc; OdbcConnection conn = new OdbcConnection(); conn.ConnectionString = "Driver={SQL Server};" + "Server=ServerName;" + "DataBase=DataBaseName;" + "Uid=;" + "Pwd=;"; conn.Open(); // or OdbcConnection conn = new OdbcConnection(); conn.ConnectionString = "Driver={SQL Server};" + "Server=ServerName;" + "DataBase=DataBaseName;" + "Trusted_Connection=Yes;"; conn.Open(); // OleDb -- Standard Connection using System.Data.OleDb; OleDbConnection conn = new OleDbConnection(); conn.ConnectionString = "Driver=SQLOLEDB;" + "Data Source=ServerName;" + "Initial Catalog=DataBaseName;" + "User id=UserName;" + "Password=Secret;"; conn.Open(); // OleDb -- Trusted Connection using System.Data.OleDb; OleDbConnection conn = new OleDbConnection(); conn.ConnectionString = "Driver=SQLOLEDB;" + "Data Source=ServerName;" + "Initial Catalog=DataBaseName;" + "Integrated Security=SSPI;"; conn.Open(); // OleDb -- via IP Address using System.Data.OleDb; OleDbConnection conn = new OleDbConnection(); conn.ConnectionString = "Driver=SQLOLEDB;" + "Network Library=DBMSSOCN;" + "Data Source=xxx.xxx.xxx.xxx,1433;" + "Initial Catalog=DataBaseName;" + "User id=UserName;" + "Password=Secret;"; conn.Open(); // .NET DataProvider -- Standard Connection using System.Data.SqlClient; SqlConnection conn = new SqlDbConnection(); conn.ConnectionString = "Data Source=ServerName;" + "Initial Catalog=DataBaseName;" + "User id=UserName;" + "Password=Secret;"; conn.Open(); // .NET DataProvider -- Trusted Connection using System.Data.SqlClient; SqlConnection conn = new SqlConnection(); conn.ConnectionString = "Data Source=ServerName;" + "Initial Catalog=DataBaseName;" + "Integrated Security=SSPI;"; conn.Open(); // .NET DataProvider -- via IP Address using System.Data.SqlClient; SqlConnection conn = new SqlConnection(); conn.ConnectionString = "Network Library=DBMSSOCN;" + "Data Source=xxx.xxx.xxx.xxx,1433;" + "Initial Catalog=DataBaseName;" + "User Id=UserName;" + "Password=Secret;"; conn.Open();
Microsoft Access 数据库连接字符串
Microsoft Access // ODBC DSN using System.Data.Odbc; OdbcConnection conn = new OdbcConnection(); conn.ConnectionString = "Dsn=DsnName"; conn.Open(); // ODBC -- Standard Security using System.Data.Odbc; OdbcConnection conn = new OdbcConnection(); conn.ConnectionString = "Driver={Microsoft Access Driver (*.mdb)};" + "Dbq=c:\myPath\myDb.mdb;" + "Uid=Admin;Pwd=;"; conn.Open(); // ODBC -- Workgroup (System Database) using System.Data.Odbc; OdbcConnection conn = new OdbcConnection(); conn.ConnectionString = "Driver={Microsoft Access Driver (*.mdb)};" + "Dbq=c:\myPath\myDb.mdb;" + "SystemDb=c:\myPath\myDb.mdw;"; conn.Open(); // ODBC -- Exclusive Use using System.Data.Odbc; OdbcConnection conn = new OdbcConnection(); conn.ConnectionString = "Driver={Microsoft Access Driver (*.mdb)};" + "Dbq=c:\myPath\myDb.mdb;" + "Exclusive=1;"; "Uid=Admin;Pwd=;"; conn.Open(); // OleDb with MS Jet -- Standard Security using System.Data.OleDb; OleDbConnection conn = new OleDbConnection(); conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=c:\mypath\myDb.mdb;" + "User id=admin;" + "Password="; conn.Open(); // OleDb with MS Jet -- Workgroup (System Database) using System.Data.OleDb; OleDbConnection conn = new OleDbConnection(); conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=c:\mypath\myDb.mdb;" + "System Database=c:\mypath\myDb.mdw;"; conn.Open(); // OleDb with MS Jet -- With Password using System.Data.OleDb; OleDbConnection conn = new OleDbConnection(); conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=c:\mypath\myDb.mdb;" + "Database Password=Secret;" conn.Open();
Microsoft Excel 连接字符串
Excel // ODBC DSN using System.Data.Odbc; OdbcConnection conn = new OdbcConnection(); conn.ConnectionString = "Dsn=DsnName;" + "Uid=UserName;" + "Pwd=Secret;"; conn.Open(); // ODBC without DSN using System.Data.Odbc; OdbcConnection conn = new OdbcConnection(); conn.ConnectionString = "Driver={Microsoft Excel Driver (*.xls)};" + "Driverid=790;" + "Dbq=C:\MyPath\SpreadSheet.xls;" + "DefaultDir=C:\MyPath;"; conn.Open(); // OleDb with MS Jet using System.Data.OleDb; OleDbConnection conn = new OleDbConnection(); conn.ConnectionString = "Driver=Microsoft.Jet.OLEDB.4.0;" + "Data Source=C:\MyPath\SpreadSheet.xls;" + @"Extended Properties=""Excel 8.0;HDR=Yes"""; conn.Open();
Text 连接字符串
Text // ODBC DSN using System.Data.Odbc; OdbcConnection conn = new OdbcConnection(); conn.ConnectionString = "Dsn=DsnName;" + "Uid=UserName;" + "Pwd=Secret;"; conn.Open(); // ODBC without DSN using System.Data.Odbc; OdbcConnection conn = new OdbcConnection(); conn.ConnectionString = "Driver={Microsoft Text Driver (*.txt; *.csv)};" + "Dbq=C:\MyPath\;" + "Extensions=asc,csv,tab,txt;"; conn.Open(); // Use: sql = "Select * From MyTextFile.txt" // OleDb with MS Jet using System.Data.OleDb; OleDbConnection conn = new OleDbConnection(); conn.ConnectionString = "Driver=Microsoft.Jet.OLEDB.4.0;" + "Data Source=C:\MyPath\;" + "Extended Properties=" + @"""text;HDR=Yes;FMT=Delimited"""; conn.Open(); // Use: sql = "Select * From MyTextFile.txt"