数据库的连接串大全
- 格式:doc
- 大小:51.00 KB
- 文档页数:13
【SQL SERVER】Standard SecurityData Source=myServerAddress;Initial Catalog=myDataBase;UserId=myUsername;Password=myPassword;Standard Security alternative syntaxThis connection string produces the same result as the previous one. The reason to include it is to point out that some connection string keywords have many equivalents.Server=myServerAddress;Database=myDataBase;UserID=myUsername;Password=myPassword;Trusted_Connection=False;Trusted ConnectionData Source=myServerAddress;Initial Catalog=myDataBase;IntegratedSecurity=SSPI;Trusted Connection alternative syntaxThis connection string produce the same result as the previous one. The reason to include it is to point out that some connection string keywords have many equivalents.Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;Use serverName\instanceName as Data Source to use a specific SQL Server instance. Please note that the multiple SQL Server instances feature is available only from SQL Server version 2000 and not in any previous versions.Connecting to an SQL Server instanceThe syntax of specifying the server instance in the value of the server key is the same for all connection strings for SQL Server.Server=myServerName\theInstanceName;Database=myDataBase;Trusted_Connection=T rue;Trusted Connection from a CE deviceOften a Windows CE device is not authenticated and logged in to a domain. To use SSPI or trusted connection / authentication from a CE device, use this connection string.Data Source=myServerAddress;Initial Catalog=myDataBase;IntegratedSecurity=SSPI;User ID=myDomain\myUsername;Password=myPassword;Note that this will only work on a CE device.Read more about connecting to SQL Server from CE devices hereConnect via an IP addressData Source=190.190.200.100,1433;Network Library=DBMSSOCN;InitialCatalog=myDataBase;User ID=myUsername;Password=myPassword;Specifying packet sizeServer=myServerAddress;Database=myDataBase;UserID=myUsername;Password=myPassword;Trusted_Connection=False;Packet Size=4096; By default, the Microsoft .NET Framework Data Provider for SQL Server sets the network packet size to 8192 bytes. This might however not be optimal, try to set this value to 4096 instead.The default value of 8192 might cause errors as well ("Failed to reserve contiguous memory"), check this outMicrosoft OLE DB Provider for SQL Serv erType: OLE DB ProviderUsage:Provider=sqloledbManufacturer: MicrosoftMore info about this provider »Customize stringexample values »Standard SecurityProvider=sqloledb;Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;Trusted connectionProvider=sqloledb;Data Source=myServerAddress;InitialCatalog=myDataBase;Integrated Security=SSPI;Use serverName\instanceName as Data Source to use a specific SQL Server instance. Please note that the multiple SQL Server instances feature is available only from SQL Server version 2000 and not in any previous versions.Connecting to an SQL Server instanceThe syntax of specifying the server instance in the value of the server key is the same for all connection strings for SQL Server.Provider=sqloledb;Data Source=myServerName\theInstanceName;InitialCatalog=myDataBase;Integrated Security=SSPI;Prompt for username and passwordThis one is a bit tricky. First set the connection object's Provider property to "sqloledb". Thereafter set the connection object's Prompt property to adPromptAlways. Then use the connection string to connect to the database.oConn.Provider = "sqloledb"oConn.Properties("Prompt") = adPromptAlwaysData Source=myServerAddress;Initial Catalog=myDataBase;Connect via an IP addressProvider=sqloledb;Data Source=190.190.200.100,1433;NetworkLibrary=DBMSSOCN;Initial Catalog=myDataBase;UserID=myUsername;Password=myPassword;DBMSSOCN=TCP/IP. This is how to use TCP/IP instead of Named Pipes. At the end of the Data Source is the port to use. 1433 is the default port for SQL Server.How to define which network protocol to useDisable connection poolingThis one is usefull when receving errors "sp_setapprole was not invoked correctly." (7.0) or "General network error. Check your network documentation" (2000) when connecting using an application role enabled connection. Application pooling (or OLE DB resource pooling) is on by default. Disabling it can help on this error.Provider=sqloledb;Data Source=myServerAddress;Initial Catalog=myDataBase;User ID=myUsername;Password=myPassword;OLE DB Services=-2;.NET Framework Data Provider for OLE DBType: .NET Framework Wrapper Class LibraryUsage:System.Data.OleDb.OleDbConnectionManufacturer: MicrosoftMore info about this wrapper class library »Customize stringexample values »Bridging to OLE DB Provider for SQL ServerThis is just one connection string sample for the wrapping OleDbConnection class that calls the underlying OLEDB provider. See respective OLE DB provider for more connection strings to use with this class.Provider=SQLOLEDB;Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername; Password=myPassword;Microsoft SQL Server ODBC DriverType: ODBC DriverUsage:Driver={SQL Server}Manufacturer: MicrosoftCustomize stringexample values »Standard SecurityDriver={SQLServer};Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPass word;Trusted connectionDriver={SQLServer};Server=myServerAddress;Database=myDataBase;Trusted_Connection=Yes;Prompt for username and passwordThis one is a bit tricky. First you need to set the connection object's Prompt property to adPromptAlways. Then use the connection string to connect to the database.oConn.Properties("Prompt") = adPromptAlwaysDriver={SQL Server};Server=myServerAddress;Database=myDataBase;SQL Server Native Client 10.0 OLE DB ProviderType: OLE DB ProviderUsage:Provider=SQLNCLI10Manufacturer: MicrosoftMore info about this provider »Customize stringexample values »Standard securityNote that the SQL Server Native Client OLE DB Provider does not support SQL Server 7.0. Provider=SQLNCLI10;Server=myServerAddress;Database=myDataBase;Uid=myUsername; Pwd=myPassword;Trusted connectionProvider=SQLNCLI10;Server=myServerAddress;Database=myDataBase;Trusted_Connection=yes;Equivalent key-value pair: "Integrated Security=SSPI" equals "Trusted_Connection=yes"Connecting to an SQL Server instanceThe syntax of specifying the server instance in the value of the server key is the same for all connection strings for SQL Server.Provider=SQLNCLI10;Server=myServerName\theInstanceName;Database=myDataBase; Trusted_Connection=yes;Prompt for username and passwordThis one is a bit tricky. First you need to set the connection object's Prompt property to adPromptAlways. Then use the connection string to connect to the database.oConn.Properties("Prompt") = adPromptAlwaysoConn.Open "Provider=SQLNCLI10;Server=myServerAddress;DataBase=myDataBase;Encrypt data sent over networkProvider=SQLNCLI10;Server=myServerAddress;Database=myDataBase;Trusted_Connection=yes;Encrypt=yes;SQL Native Client 9.0 OLE DB providerType: OLE DB ProviderUsage:Provider=SQLNCLIManufacturer: MicrosoftMore info about this provider »Customize stringexample values »Standard securityNote that the SQL Server Native Client OLE DB Provider does not support SQL Server 7.0. Provider=SQLNCLI;Server=myServerAddress;Database=myDataBase;Uid=myUsername; Pwd=myPassword;Trusted connectionProvider=SQLNCLI;Server=myServerAddress;Database=myDataBase;Trusted_Connection=yes;Equivalent key-value pair: "Integrated Security=SSPI" equals "Trusted_Connection=yes"Connecting to an SQL Server instanceThe syntax of specifying the server instance in the value of the server key is the same for all connection strings for SQL Server.Provider=SQLNCLI;Server=myServerName\theInstanceName;Database=myDataBase; Trusted_Connection=yes;Prompt for username and passwordThis one is a bit tricky. First you need to set the connection object's Prompt property to adPromptAlways. Then use the connection string to connect to the database.oConn.Properties("Prompt") = adPromptAlwaysoConn.Open "Provider=SQLNCLI;Server=myServerAddress;DataBase=myDataBase;Encrypt data sent over networkProvider=SQLNCLI;Server=myServerAddress;Database=myDataBase;Trusted_Connection=yes;Encrypt=yes;SQL Server Native Client 10.0 ODBC DriverType: ODBC DriverUsage:Driver={SQL Server Native Client 10.0}Manufacturer: MicrosoftMore info about this driver »Customize stringexample values »Standard securityDriver={SQL Server Native Client10.0};Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPasswo rd;Trusted ConnectionDriver={SQL Server Native Client10.0};Server=myServerAddress;Database=myDataBase;Trusted_Connection=yes; Equivalent key-value pair: "Integrated Security=SSPI" equals "Trusted_Connection=yes"Connecting to an SQL Server instanceThe syntax of specifying the server instance in the value of the server key is the same for all connection strings for SQL Server.Driver={SQL Server Native Client 10.0};Server=myServerName\theInstanceName; Database=myDataBase;Trusted_Connection=yes;Prompt for username and passwordThis one is a bit tricky. First you need to set the connection object's Prompt property to adPromptAlways. Then use the connection string to connect to the database.oConn.Properties("Prompt") = adPromptAlwaysDriver={SQL Server Native Client10.0};Server=myServerAddress;Database=myDataBase;Encrypt data sent over networkDriver={SQL Server Native Client10.0};Server=myServerAddress;Database=myDataBase;Trusted_Connection=yes;Encrypt=yes;SQL Native Client 9.0 ODBC DriverType: ODBC DriverUsage:Driver={SQL Native Client}Manufacturer: MicrosoftMore info about this driver »Customize stringexample values »Standard securityDriver={SQL Native Client};Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;Trusted ConnectionDriver={SQL Native Client};Server=myServerAddress;Database=myDataBase; Trusted_Connection=yes;Equivalent key-value pair: "Integrated Security=SSPI" equals "Trusted_Connection=yes"Connecting to an SQL Server instanceThe syntax of specifying the server instance in the value of the server key is the same for all connection strings for SQL Server.Driver={SQL NativeClient};Server=myServerName\theInstanceName;Database=myDataBase;Trusted_Connection=yes;Prompt for username and passwordThis one is a bit tricky. First you need to set the connection object's Prompt property to adPromptAlways. Then use the connection string to connect to the database.oConn.Properties("Prompt") = adPromptAlwaysDriver={SQL Native Client};Server=myServerAddress;Database=myDataBase;Customize stringexample values »MSDataShapeProvider=MSDataShape;Data Provider=SQLOLEDB;DataSource=myServerAddress;Initial Catalog=myDataBase;UserID=myUsername;Password=myPassword;【MYSQL】StandardServer=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword; Default port is 3306.Specifying portServer=myServerAddress;Port=1234;Database=myDataBase;Uid=myUsername;Pwd=myPa ssword;Download the driver at MySQL Developer ZoneNamed pipesServer=myServerAddress;Port=-1;Database=myDataBase;Uid=myUsername;Pwd=myPass word;It is the port value of -1 that tells the driver to use named pipes network protocol. This is available on Windows only. The value is ignored if Unix socket is used.Multiple serversUse this to connect to a server in a replicated server configuration without concern on which server to use.Server=serverAddress1 & serverAddress2 &etc..;Database=myDataBase;Uid=myUsername;Pwd=myPassword;Using encryptionThis one activates SSL encryption for all data sent between the client and server. The server needs to have a certificate installed.Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;Enc ryption=true;This option is available from Connector/NET version 5.0.3. In earlier versions, this option has no effect.Using encryption, alternativeSome reported problems with the above one. Try replacing the key "Encryption" with "Encrypt" instead.Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;Enc rypt=true;Specifying default command timeoutUse this one to specify a default command timeout for the connection. Please note that the property in the connection string does not supercede the individual command timeout property on an individual command object.Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;def ault command timeout=20;This option is available from Connector/NET version 5.1.4.Specifying connection attempt timeoutUse this one to specify the length in seconds to wait for a server connection before terminating the attempt and receive an error.Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;Con nection Timeout=5;Inactivating prepared statementsUse this one to instruct the provider to ignore any command prepare statements and prevent corruption issues with server side prepared statements.Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;Ign ore Prepare=true;The option was added in Connector/NET version 5.0.3 and Connector/NET version 1.0.9.Specifying portUse this one to specify what port to use for the connection.Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;Por t=3306;The port 3306 is the default MySql port.The value is ignored if Unix socket is used.Specifying network protocolUse this one to specify which network protocol to use for the connection.Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword; Protocol=socket;"socket" is the default value used if the key isn't specified. Value "tcp" is an equivalent for "socket".Manufacturer: MySQLMore info about this driver »Customize stringexample values »Local databaseDriver={MySQL ODBC 3.51 Driver};Server=localhost;Database=myDataBase;User=myUsername;Password=myPassword;Option=3;Remote databaseDriver={MySQL ODBC 3.51Driver};Server=myServerAddress;Database=myDataBase;User=myUsername;Password=myPassword;Option=3;Specifying TCP/IP portDriver={MySQL ODBC 3.51Driver};Server=myServerAddress;Port=3306;Database=myDataBase;User=myUsername; Password=myPassword;Option=3;The driver defaults to port value 3306, if not specified in the connection string, as 3306 is the default port for MySQL.Specifying character setDriver={MySQL ODBC 3.51Driver};Server=myServerAddress;charset=UTF8;Database=myDataBase;User=myUsern ame; Password=myPassword;Option=3;Note that the charset option works from version 3.51.17 of the driver.Specifying socketThis one specifies the Unix socket file or Windows named pipe to connect to. Used only for local client connections.Driver={MySQL ODBC 3.51Driver};Server=myServerAddress;Database=myDataBase;User=myUsername;Password=myPassword;Socket=MySQL;Option=3;On Windows, the socket variable is the name of the named pipe that is used for local client connections. The default value is MySQL.On Unix platforms, the socket variable is the name of the socket file that is used for local client connections. The default is /tmp/mysql.sock.Using SSLDriver={MySQL ODBC 3.51Driver};Server=myServerAddress;Database=myDataBase;User=myUsername;Password=myPassword;sslca=c:\cacert.pem;sslcert=c:\client-cert.pem;sslkey=c: \client-key.pem;sslverify=1;Option=3;SSLCA specifies the path to a file with a list of trust SSL CAsSSLCERT specifies the name of the SSL certificate file to use for establishing a secure connection.SSLKEY specifies the name of the SSL key file to use for establishing a secure connection. MySQL Connector/ODBC 5.1Type: ODBC DriverUsage:Driver={MySQL ODBC 5.1 Driver}Manufacturer: MySQLMore info about this driver »Customize stringexample values »Local databaseDriver={MySQL ODBC 5.1 Driver};Server=localhost;Database=myDataBase;User=myUsername;Password=myPassword;Option=3;Remote databaseDriver={MySQL ODBC 5.1Driver};Server=myServerAddress;Database=myDataBase;User=myUsername;Password=myPassword;Option=3;Specifying TCP/IP portDriver={MySQL ODBC 5.1Driver};Server=myServerAddress;Port=3306;Database=myDataBase;User=myUsername; Password=myPassword;Option=3;The driver defaults to port value 3306, if not specified in the connection string, as 3306 is the default port for MySQL.Specifying character setDriver={MySQL ODBC 5.1Driver};Server=myServerAddress;charset=UTF8;Database=myDataBase;User=myUsern ame; Password=myPassword;Option=3;Specifying socketThis one specifies the Unix socket file or Windows named pipe to connect to. Used only for local client connections.Driver={MySQL ODBC 5.1Driver};Server=myServerAddress;Database=myDataBase;User=myUsername;Password=myPassword;Socket=MySQL;Option=3;On Windows, the socket variable is the name of the named pipe that is used for local client connections. The default value is MySQL.On Unix platforms, the socket variable is the name of the socket file that is used for local client connections. The default is /tmp/mysql.sock.Using SSLDriver={MySQL ODBC 5.1Driver};Server=myServerAddress;Database=myDataBase;User=myUsername;Password=myPassword;sslca=c:\cacert.pem;sslcert=c:\client-cert.pem;sslkey=c: \client-key.pem;sslverify=1;Option=3;SSLCA specifies the path to a file with a list of trust SSL CAsSSLCERT specifies the name of the SSL certificate file to use for establishing a secure connection.SSLKEY specifies the name of the SSL key file to use for establishing a secure connection..NET Framework Data Provider for ODBCType: .NET Framework Wrapper Class LibraryUsage:System.Data.Odbc.OdbcConnectionManufacturer: MicrosoftMore info about this wrapper class library »Customize stringexample values »Bridging to MySQL Connector/ODBC 5.1This is just one connection string sample for the wrapping OdbcConnection class that calls the underlying ODBC Driver. See respective ODBC driver for more connection strings to use with this class.Driver={MySQL ODBC 5.1 Driver};Server=localhost;Database=myDataBase;User=myUsername;Password=myPassword;Option=3;。
opengauss数据库连接串的参考样例
在OpenGauss数据库中,连接串用于建立与数据库的连接。
连接串通常包括数据库的地址、端口、用户名、密码等信息。
以下是一个OpenGauss数据库连接串的参考样例:
```
jdbc:opengauss://hostname:port/databasename?user=username&password=password
```
其中,各部分的含义如下:
- `jdbc:opengauss://`:表示使用OpenGauss JDBC驱动。
- `hostname`:数据库服务器的主机名或IP地址。
- `port`:数据库服务器的端口号,默认为`5432`。
- `databasename`:要连接的数据库的名称。
- `user`:连接数据库的用户名。
- `password`:连接数据库的密码。
示例:
```plaintext
jdbc:opengauss://localhost:5432/mydatabase?user=myuser&password=mypassword
```
在实际使用时,请替换为你的实际数据库主机名、端口号、数据库名称、用户名和密码。
此外,OpenGauss还支持SSL连接,你可以在连接串中添加SSL相关的配置参数,以加强连接的安全性。
数据库内连接查询语句数据库内连接查询语句是数据库中常用的一种查询方法,用于从多个表中检索数据并进行关联。
下面是十个符合要求的数据库内连接查询语句的示例:1. 查询订单表和客户表中的所有匹配记录:SELECT * FROM 订单表 INNER JOIN 客户表 ON 订单表.客户ID = 客户表.客户ID;2. 查询学生表和课程表中的所有匹配记录:SELECT * FROM 学生表 INNER JOIN 课程表 ON 学生表.学生ID = 课程表.学生ID;3. 查询员工表和部门表中的所有匹配记录:SELECT * FROM 员工表 INNER JOIN 部门表 ON 员工表.部门ID = 部门表.部门ID;4. 查询商品表和分类表中的所有匹配记录:SELECT * FROM 商品表 INNER JOIN 分类表 ON 商品表.分类ID = 分类表.分类ID;5. 查询订单表、客户表和商品表中的所有匹配记录:SELECT * FROM 订单表 INNER JOIN 客户表 ON 订单表.客户ID = 客户表.客户ID INNER JOIN 商品表 ON 订单表.商品ID = 商品表.商品ID;6. 查询学生表、课程表和成绩表中的所有匹配记录:SELECT * FROM 学生表 INNER JOIN 课程表 ON 学生表.学生ID = 课程表.学生ID INNER JOIN 成绩表 ON 学生表.学生ID = 成绩表.学生ID;7. 查询员工表、部门表和工资表中的所有匹配记录:SELECT * FROM 员工表 INNER JOIN 部门表 ON 员工表.部门ID = 部门表.部门ID INNER JOIN 工资表 ON 员工表.员工ID = 工资表.员工ID;8. 查询商品表、分类表和库存表中的所有匹配记录:SELECT * FROM 商品表 INNER JOIN 分类表 ON 商品表.分类ID = 分类表.分类ID INNER JOIN 库存表 ON 商品表.商品ID = 库存表.商品ID;9. 查询订单表和客户表中匹配的记录,并按照订单金额升序排序:SELECT * FROM 订单表 INNER JOIN 客户表 ON 订单表.客户ID = 客户表.客户ID ORDER BY 订单表.订单金额 ASC;10. 查询学生表和课程表中匹配的记录,并按照课程名称降序排序:SELECT * FROM 学生表 INNER JOIN 课程表 ON 学生表.学生ID = 课程表.学生ID ORDER BY 课程表.课程名称 DESC;以上是十个符合要求的数据库内连接查询语句的示例,它们可以用于从多个表中检索数据并进行关联。
C#各种数据库连接字符串⼤全——SQLServer、Oracle、Access 刚开始学习C#开发项⽬时,长期会为了写⼀个安全的⾼效的数据库连接发愁。
我发现现在很多刚开始学习的朋友和有些做了⼀两年的朋友,也有点犯愁,我就贴上这个代码,给⾃⼰以后做个参考,也给朋友做个学习的参考。
我主要在这⾥统计了.NET项⽬下常⽤的数据库连接字符串:SQLServer、Oracle、Access三种数据库(MySql、SQLLite、Excel、HTML Table等等暂不列⼊)。
⼀、常⽤连接字符串参数说明如需查看详细说明请参见:关键字默认描述Server 或 Data Source N/A要连接的数据库实例的名称或⽹络地址(可以在名称后指定端⼝号),指定本地实例可⽤(Local),如果是SqlExpress(名称\SqlExpress)。
Initial Catalog 或 Database N/A数据库的名称。
User ID 或 UID N/A登录帐户。
Password 或 Pwd N/A帐户登录的密码。
Persist Security Info 'false'当该值设置为 false 或 no(强烈推荐)时,如果连接是打开的或者⼀直处于打开状态,那么安全敏感信息(如密码)将不会作为连接的⼀部分返回。
重置连接字符串将重置包括密码在内的所有连接字符串值。
可识别的值为 true、false、yes 和 no。
Enlist 'false'true 表明连接池程序在创建线程的当前事务上下⽂中⾃动登记连接。
可识别的值为 true、false、yes 和 no。
Connection Lifetime 0当连接被返回到池时,将其创建时间与当前时间作⽐较,如果时间长度(以秒为单位)超出了由 Connection Lifetime 指定的值,该连接就会被销毁。
这在聚集配置中很有⽤(⽤于强制执⾏运⾏中的服务器和刚置于联机状态的服务器之间的负载平衡)。
几种常见的数据库连接方法一、连接Access数据库1.使用已有DSN的连接字符串进行连接(ODBC)使用DSN进行连接〃导入命名空间using System.Data.Odbc;protected void Page_Load(Object sender,EventArgs e)(〃设置连接字符串String connstr=@"DSN=sample";〃实例化Connection对象OdbcConnection myConnection = new OdbcConnection(connstr);〃执行Open方法打开连接myConnection.Open();〃执行SQL语句OdbcCommand myCommand new OdbcCommand("select * from sampletable",myConnection);〃将查询的结果赋给GridView的数据源gv.DataSource = myCommand.ExecuteReader();〃绑定GridViewgv.DataBind();〃关闭连接myConnection.Close();)2.使用无DSN的连接字符串进行连接(ODBC)不使用DSN进行连接〃导入命名空间using System.Data.Odbc;protected void Page_Load(Object sender,EventArgs e)(〃设置连接字符串String connstr=@"Driver=Microsoft Access Driver (*.mdb);Dbq=c:\sample.mdb;";〃实例化Connection对象OdbcConnection myConnection = new OdbcConnection(connstr);〃执行Open方法打开连接myConnection.Open();〃执行SQL语句OdbcCommand myCommand new OdbcCommand("select * from sampletable",myConnection);〃将查询的结果赋给GridView的数据源gv.DataSource = myCommand.ExecuteReader();〃绑定GridViewgv.DataBind();〃关闭连接myConnection.Close();)3.使用连接字符串进行连接(OLEDB) Data Provider 支持的OLEDB Provider:SQLOLEDB:用来访问SQL Server数据库MSDAORA:用来访问Oracle数据库Microsoft.Jet.OLEDB.4.0:用来访问Access 数据库。
数据库外连接语句
数据库外连接语句
外连接(Outer Joins)被用来从一个(或多个)表中取得与另一个表中的数据匹配的行。
另外,它还可以从表中取得非匹配行。
一.左外连接(LEFT OUTER JOIN)
例如:
SELECT stName, Employees.FirstName, Orders.OrderID
FROM Employees LEFT OUTER JOIN Orders ON
Employees.EmployeeID = Orders.EmployeeID
这条语句会返回所有员工的订单信息,如果该员工没有订单,系统也会返回该员工的信息,只是订单号为空。
二.右外连接(RIGHT OUTER JOIN)
例如:
SELECT stName, Employees.FirstName, Orders.OrderID
FROM Employees RIGHT OUTER JOIN Orders ON
Employees.EmployeeID = Orders.EmployeeID
这条语句会返回所有订单信息,如果该订单没有员工,系统也会返回该订单的信息,只是员工的信息为空。
三.完全外连接(FULL OUTER JOIN)
例如:
SELECT stName, Employees.FirstName, Orders.OrderID
FROM Employees FULL OUTER JOIN Orders ON
Employees.EmployeeID = Orders.EmployeeID
这条语句会返回所有员工及订单信息,如果没有匹配的信息,系统也会返回相应的信息,只是关联列对应的信息为空。
连接数据库查询语句
连接数据库并进行查询通常需要使用特定的数据库查询语句,这取决于你所使用的数据库管理系统。
以下是一些常见的数据库查询语句示例:
1. 对于MySQL数据库:
连接到数据库,`mysql -u 用户名 -p 密码 -h 主机名数据库名`。
查询数据,`SELECT FROM 表名 WHERE 条件`。
2. 对于Oracle数据库:
连接到数据库,`sqlplus 用户名/密码@主机名:端口/服务名`。
查询数据,`SELECT FROM 表名 WHERE 条件`。
3. 对于SQL Server数据库:
连接到数据库,`sqlcmd -S 服务器名 -U 用户名 -P 密码
-d 数据库名`。
查询数据,`SELECT FROM 表名 WHERE 条件`。
4. 对于PostgreSQL数据库:
连接到数据库,`psql -h 主机名 -U 用户名 -d 数据库名`。
查询数据,`SELECT FROM 表名 WHERE 条件`。
无论使用哪种数据库管理系统,查询语句的核心部分都是
`SELECT FROM 表名 WHERE 条件`,其中`SELECT`用于选择要检索
的列,`FROM`用于指定要检索数据的表,`WHERE`用于过滤检索的数据。
在使用数据库查询语句时,需要确保对数据库有足够的权限,
并且要谨慎处理敏感信息,以免造成数据泄露或损坏。
同时,还应
该考虑到查询的性能,避免对数据库造成过大的负担。
总之,连接数据库并进行查询是数据库管理和开发中非常常见
的操作,需要根据具体的情况选择合适的数据库查询语句,并且要注意安全和性能方面的考虑。
Oracle数据库连接字符串(经典大全)2009-08-21 14:36ODBC新版本Driver={Microsoft ODBC for Oracle};Server=myServerAddress;Uid=myUsername;Pwd=myPassword;旧版本Driver={Microsoft ODBC Driver for Oracle};ConnectString=OracleServer.world;Uid=myUsername;Pwd=myPassword; OLE DB, OleDbConnection (.NET)标准连接此连接字符串适用了微软的驱动。
Provider=msdaora;Data Source=MyOracleDB;User Id=myUsername;Password=myPassword;受信连接Provider=msdaora;Data Source=MyOracleDB;Persist Security Info=False;Integrated Security=Yes;标准连接由Oracle提供的驱动。
Provider=OraOLEDB.Oracle;Data Source=MyOracleDB;User Id=myUsername;Password=myPassword;受信连接Provider=OraOLEDB.Oracle;Data Source=MyOracleDB;OSAuthent=1;Oracle.DataAccess.Client.OracleConnectionData Source=TORCL;User Id=myUsername;Password=myPassword;标准安全连接Data Source=TORCL;Integrated Security=SSPI;使用而不使用tnsnames.oraDataSource=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=MyHost)(PORT=MyPort)))(C ONNECT_DATA=(SERVER=DEDICA TED)(SERVICE_NAME=MyOracleSID)));UserId=myUsername;Password=myPassword;OracleConnection, Oracle Data Provider, , System.Data.OracleClient.OracleConnection标准Data Source=MyOracleDB;Integrated Security=yes;用于8i RC3及以后的版本指定用户名和密码Data Source=MyOracleDB;User Id=myUsername;Password=myPassword;Integrated Security=no;用于8i RC3及以后的版本忽略tnsnames.ora另一种不需要使用DSN的连接方式。
sql 连接数据库语句在使用SQL连接数据库时,可以使用以下语句进行连接:1. 使用MySQL连接数据库:```mysql -h 主机名 -P 端口号 -u 用户名 -p```这里的主机名是指要连接的数据库服务器的主机名,端口号是指数据库服务器的端口号,用户名是指要连接的数据库的用户名,密码是指要连接的数据库的密码。
2. 使用PostgreSQL连接数据库:```psql -h 主机名 -p 端口号 -U 用户名 -W```这里的主机名是指要连接的数据库服务器的主机名,端口号是指数据库服务器的端口号,用户名是指要连接的数据库的用户名,-W 选项表示需要输入密码进行连接。
3. 使用Oracle连接数据库:```sqlplus 用户名/密码@主机名:端口号/服务名```这里的用户名是指要连接的数据库的用户名,密码是指要连接的数据库的密码,主机名是指要连接的数据库服务器的主机名,端口号是指数据库服务器的端口号,服务名是指要连接的数据库的服务名。
4. 使用SQL Server连接数据库:```sqlcmd -S 服务器名 -U 用户名 -P 密码 -d 数据库名```这里的服务器名是指要连接的数据库服务器的服务器名,用户名是指要连接的数据库的用户名,密码是指要连接的数据库的密码,数据库名是指要连接的数据库的数据库名。
5. 使用SQLite连接数据库:```sqlite3 数据库文件名```这里的数据库文件名是指要连接的SQLite数据库文件的文件名。
6. 使用MariaDB连接数据库:```mysql -h 主机名 -P 端口号 -u 用户名 -p```这里的主机名是指要连接的数据库服务器的主机名,端口号是指数据库服务器的端口号,用户名是指要连接的数据库的用户名,密码是指要连接的数据库的密码。
7. 使用DB2连接数据库:```db2 connect to 数据库名 user 用户名 using 密码```这里的数据库名是指要连接的数据库的数据库名,用户名是指要连接的数据库的用户名,密码是指要连接的数据库的密码。
一、.NET Framework Data Provider for SQL Server类型:.NET Framework类库使用:System.Data.SqlClient.SqlConnection厂商:Microsoft1.标准安全连接复制代码代码如下:Data Source = myServerAddress;Initial Catalog = myDataBase;User Id = myUsername;Password = myPassword;使用服务器名\实例名作为连接指定SQL Server实例的数据源。
如果你使用的是SQL Server 2008 Express版,实例名为SQLEXPRESS。
2.可替代的标准安全连接复制代码代码如下:Server = myServerAddress;Database = myDataBase;User ID = myUsername;Password = myPassword;Trusted_Connection = False;这条连接字符串跟上一条效果一样。
把这条写出来只是想说,其实很多连接字符串的关键字有多种写法。
3.信任连接复制代码代码如下:Data Source = myServerAddress;Initial Catalog = myDataBase;Integrated Security = SSPI;可替代的信任连接复制代码代码如下:Server = myServerAddress;Database = myDataBase;Trusted_Connection = True;4.连接Windows CE设备的信任连接通常一台Windows CE设备在一个域里是不能被认证和登录的。
为了让一台CE设备使用SSPI或信任连接和认证,可以使用下面的连接字符串:复制代码代码如下:Data Source = myServerAddress;Initial Catalog = myDataBase;Integrated Security = SSPI;User ID = myDomain\myUsername;Password = myPassword;说明一下,这条语句只能在CE设备上用。
(最全的数据库连接字符串)connectionstringPS:如果不是太稳定的数据库,最好使用connection lifetime=10来限制连接池内连接的生存日期转自SQL ServerODBCStandard Security:"Driver={SQLServer};Server=Aron1;Database=pubs;Uid=sa;Pwd=asdasd;"Trusted connection:"Driver={SQLServer};Server=Aron1;Database=pubs;Trusted_Connection=yes;"Prompt for username and password:oConn.Properties("Prompt") = adPromptAlwaysoConn.Open "Driver={SQL Server};Server=Aron1;DataBase=pubs;"OLE DB, OleDbConnection (.NET)Standard Security:"Provider=sqloledb;Data Source=Aron1;Initial Catalog=pubs;User Id=sa;Password=asdasd;"Trusted Connection:"Provider=sqloledb;Data Source=Aron1;Initial Catalog=pubs;Integrated Security=SSPI;"(use serverName\instanceName as Data Source to use an specifik SQLServer instance, only SQLServer2000)Prompt for username and password:oConn.Provider = "sqloledb"oConn.Properties("Prompt") = adPromptAlwaysoConn.Open "Data Source=Aron1;Initial Catalog=pubs;"Connect via an IP address:"Provider=sqloledb;DataSource=190.190.200.100,1433;NetworkLibrary=DBMSSOCN;Initial Catalog=pubs;User ID=sa;Password=asdasd;"(DBMSSOCN=TCP/IP instead of Named Pipes, at the end of the Data Source is the port to use (1433 is the default))SqlConnection (.NET)Standard Security:"Data Source=Aron1;Initial Catalog=pubs;User Id=sa;Password=asdasd;"- or -"Server=Aron1;Database=pubs;UserID=sa;Password=asdasd;Trusted_Connection=False"(both connection strings produces the same result)Trusted Connection:"Data Source=Aron1;Initial Catalog=pubs;Integrated Security=SSPI;"- or -"Server=Aron1;Database=pubs;Trusted_Connection=True;"(both connection strings produces the same result)(use serverName\instanceName as Data Source to use an specifik SQLServer instance, only SQLServer2000)Connect via an IP address:"Data Source=190.190.200.100,1433;Network Library=DBMSSOCN;Initial Catalog=pubs;User ID=sa;Password=asdasd;"(DBMSSOCN=TCP/IP instead of Named Pipes, at the end of the Data Source is the port to use (1433 is the default))Declare the SqlConnection:C#:using System.Data.SqlClient;SqlConnection oSQLConn = new SqlConnection(); oSQLConn.ConnectionString="my connection string"; oSQLConn.Open();:Imports System.Data.SqlClientDim oSQLConn As SqlConnection = New SqlConnection() oSQLConn.ConnectionString="my connection string" oSQLConn.Open()Data ShapeMS Data Shape"Provider=MSDataShape;Data Provider=SQLOLEDB;Data Source=Aron1;Initial Catalog=pubs;User ID=sa;Password=asdasd;"Want to learn data shaping? Check out 4GuyfFromRolla's great article about Data Shaping >>Read moreHow to define which network protocol to useExample:"Provider=sqloledb;DataSource=190.190.200.100,1433;NetworkLibrary=DBMSSOCN;Initial Catalog=pubs;User ID=sa;Password=asdasd;"Name Network librarydbnmpntw Win32 Named Pipesdbmssocn Win32 Winsock TCP/IPdbmsspxn Win32 SPX/IPXdbmsvinn Win32 Banyan Vinesdbmsrpcn Win32 Multi-Protocol (Windows RPC)Important note!When connecting through the SQLOLEDB provider use the syntax Network Library=dbmssocnand when connecting through MSDASQL provider use the syntax Network=dbmssocnAll SqlConnection connection string propertiesThis table shows all connection string properties for the SqlConnection object. Most of the properties are also used in ADO. All properties and descriptions is from msdn.Name Default DescriptionApplication Name The name of the application, or '.Net SqlClient Data Provider' if no application name is provided.AttachDBFilename-or-extended properties-or-Initial File Name The name of the primary file, including the full path name, of an attachable database. The database name must be specified with the keyword 'database'.Connect Timeout-or-Connection Timeout 15 The length of time (in seconds) to wait for a connection to the server before terminating the attempt and generating an error.Connection Lifetime 0 When a connection is returned to the pool, its creation time is compared with the current time, and the connection is destroyed if that time span (in seconds) exceeds the value specified by connection lifetime. Useful in clustered configurations to force load balancing between a running server and a server just brought on-line.Connection Reset 'true' Determines whether the database connection is reset when being removed from the pool. Setting to 'false' avoids making an additional server round-trip when obtaining a connection, but the programmer must be aware thatthe connection state is not being reset.Current Language The SQL Server Language record name.Data Source-or-Server-or-Address-or-Addr-or-Network Address The name or network address of the instance of SQL Server to which to connect.Enlist 'true' When true, the pooler automatically enlists the connection in the creation thread's current transaction context.Initial Catalog-or-Database The name of the database.Integrated Security-or-Trusted_Connection 'false' Whether the connection is to be a secure connection or not. Recognized values are 'true', 'false', and 'sspi', which is equivalent to 'true'.Max Pool Size 100 The maximum number of connections allowed in the pool.Min Pool Size 0 The minimum number of connections allowed in the pool.Network Library-or-Net 'dbmssocn' The network library used to establish a connection to an instance of SQL Server. Supported values include dbnmpntw (Named Pipes), dbmsrpcn (Multiprotocol), dbmsadsn (Apple Talk), dbmsgnet (VIA), dbmsipcn (Shared Memory) and dbmsspxn (IPX/SPX), and dbmssocn (TCP/IP).The corresponding network DLL must be installed on the system to which you connect. If you do not specify a network and you use a local server (for example, "." or "(local)"), shared memory is used.Packet Size 8192 Size in bytes of the network packets used to communicate with an instance of SQL Server.Password-or-Pwd The password for the SQL Server account logging on.Persist Security Info 'false' When set to 'false', security-sensitive information, such as the password, is not returned as part of the connection if the connection is open or has ever been in an open state. Resetting the connection string resets all connection string values including the password.Pooling 'true' When true, the SQLConnection object is drawn from the appropriate pool, or if necessary, is created and added to the appropriate pool.User ID The SQL Server login account.Workstation ID the local computer name The name of the workstation connecting to SQL Server.NoteUse ; to separate each property.If a name occurs more than once, the value from the last one in the connection string will be used.If you are building your connection string in your app using values from user input fields, make sure the user can't change the connection string by inserting an additional property with another value within the user value.SQL Server 2005SQL Native Client ODBC DriverStandard security:"Driver={SQL Native Client};Server=Aron1;Database=pubs;UID=sa;PWD=asdasd;"Trusted connection:"Driver={SQL Native Client};Server=Aron1;Database=pubs;Trusted_Connection=yes;"EquivalentsIntegrated Security=SSPI equals Trusted_Connection=yesPrompt for username and password:oConn.Properties("Prompt") = adPromptAlwaysoConn.Open "Driver={SQL Native Client};Server=Aron1;DataBase=pubs;"Enabling MARS (multiple active result sets):"Driver={SQL Native Client};Server=Aron1;Database=pubs;Trusted_Connection=yes; MARS_Connection=yes"EquivalentsMultipleActiveResultSets=true equals MARS_Connection=yesUsing MARS with SQL Native Client, by Chris Lee >>Encrypt data sent over network:"Driver={SQL Native Client};Server=Aron1;Database=pubs;Trusted_Connection=yes;E ncrypt=yes"Attach a database file on connect to a local SQL Server Express instance:"Driver={SQL Native Client};Server=.\SQLExpress;AttachDbFilename=c:\asd\qwe\myd bfile.mdf;Database=dbname;Trusted_Connection=Yes;"- or -"Driver={SQL Native Client};Server=.\SQLExpress;AttachDbFilename=|DataDirectory| mydbfile.mdf;Database=dbname;Trusted_Connection=Yes;"(use |DataDirectory| when your database file resides in the data directory)Why is the "Database" parameter needed? Answer: If the database was previously attached, SQL Server does not reattach it (it uses the attached database as the default for the connection).Download the SQL Native Client here >> (the package contains booth the ODBC driver and the OLE DB provider)Using SQL Server 2005 Express? Don't miss the server name syntax: SERVERNAME\SQLEXPRESS (Substitute "SERVERNAME" with the name of the computer)SQL Native Client OLE DB ProviderStandard security:"Provider=SQLNCLI;Server=Aron1;Database=pubs;UID=sa;P WD=asdasd;"Trusted connection:"Provider=SQLNCLI;Server=Aron1;Database=pubs;Trusted_ Connection=yes;"EquivalentsIntegrated Security=SSPI equals Trusted_Connection=yesPrompt for username and password:oConn.Properties("Prompt") = adPromptAlwaysoConn.Open"Provider=SQLNCLI;Server=Aron1;DataBase=pubs;"Enabling MARS (multiple active result sets):"Provider=SQLNCLI;Server=Aron1;Database=pubs;Trusted_Connection=yes;MarsConn=yes"EquivalentsMarsConn=yes equals MultipleActiveResultSets=true equals MARS_Connection=yesUsing MARS with SQL Native Client, by Chris Lee >>Encrypt data sent over network:"Provider=SQLNCLI;Server=Aron1;Database=pubs;Trusted_ Connection=yes;Encrypt=yes"Attach a database file on connect to a local SQL Server Express instance:"Provider=SQLNCLI;Server=.\SQLExpress;AttachDbFilename =c:\asd\qwe\mydbfile.mdf;Database=dbname;Trusted_Connecti on=Yes;"- or -"Provider=SQLNCLI;Server=.\SQLExpress;AttachDbFilename =|DataDirectory|mydbfile.mdf;Database=dbname;Trusted_Conn ection=Yes;"(use |DataDirectory| when your database file resides in thedata directory)Why is the "Database" parameter needed? Answer: If the database was previously attached, SQL Server does not reattach it (it uses the attached database as the default for the connection).Download the SQL Native Client here >> (the package contains booth the ODBC driver and the OLE DB provider)Using SQL Server 2005 Express? Don't miss the server name syntax: SERVERNAME\SQLEXPRESS (Substitute "SERVERNAME" with the name of the computer)SqlConnection (.NET)Standard Security:"Data Source=Aron1;Initial Catalog=pubs;User Id=sa;Password=asdasd;"- or -"Server=Aron1;Database=pubs;UserID=sa;Password=asdasd;Trusted_Connection=False"(both connection strings produces the same result)Trusted Connection:"Data Source=Aron1;Initial Catalog=pubs;Integrated Security=SSPI;"- or -"Server=Aron1;Database=pubs;Trusted_Connection=True;"(both connection strings produces the same result)(use serverName\instanceName as Data Source to use an specifik SQLServer instance)Connect via an IP address:"Data Source=190.190.200.100,1433;Network Library=DBMSSOCN;Initial Catalog=pubs;User ID=sa;Password=asdasd;"(DBMSSOCN=TCP/IP instead of Named Pipes, at the end of the Data Source is the port to use (1433 is the default))Enabling MARS (multiple active result sets):"Server=Aron1;Database=pubs;Trusted_Connection=True;M ultipleActiveResultSets=true"Note! Use 2.0 for MARS functionality. MARS is not supported in 1.0 nor 1.1Streamline your Data Connections by Moving to MARS, by Laurence Moroney, >>Attach a database file on connect to a local SQL Server Express instance:"Server=.\SQLExpress;AttachDbFilename=c:\asd\qwe\mydb file.mdf;Database=dbname;Database=dbname;Trusted_Connect ion=Yes;"- or -"Server=.\SQLExpress;AttachDbFilename=|DataDirectory|m ydbfile.mdf;Database=dbname;Trusted_Connection=Yes;"(use |DataDirectory| when your database file resides in the data directory)Why is the "Database" parameter needed? Answer: If the database was previously attached, SQL Server does not reattach it (it uses the attached database as the default for the connection).Using "User Instance" on a local SQL Server Express instance:"Data Source=.\SQLExpress;integrated security=true;attachdbfilename=|DataDirectory|\mydb.mdf;user instance=true;"The "User Instance" functionality creates a new SQL Server instance on the fly during connect. This works only on a local SQL Server 2005 instance and only when connecting using windows authentication over local named pipes. The purpose is to be able to create a full rights SQL Server instance to a user with limited administrative rights on the computer. To enable the functionality: sp_configure 'user instances enabled','1' (0 to disable)Using SQL Server 2005 Express? Don't miss the server name syntax: SERVERNAME\SQLEXPRESS (Substitute "SERVERNAME" with the name of the computer)Context Connection - connecting to "self" from within your CLR stored prodedure/functionC#:using(SqlConnection connection = new SqlConnection("context connection=true")){connection.Open();// Use the connection}Visual Basic:Using connection as new SqlConnection("context connection=true")connection.Open()' Use the connectionEnd UsingThe context connection lets you execute Transact-SQL statements in the same context (connection) that your code was invoked in the first place.Read moreWhen to use SQL Native Client?.Net applicationsDo not use the SQL Native Client. Use the .NET Framework Data Provider for SQL Server (SqlConnection).COM applications, all other then .NET applicationsUse the SQL Native Client if you are accessing an SQL Server 2005 and need the new features of SQL Server 2005 such as MARS, encryption, XML data type etc. Continue use your current provider (OLE DB / ODBC through the MDAC package) if you are not connecting to an SQL Server 2005 (that's quite obvious eh..) or if you are connecting to an SQL Server 2005 but are not using any of the new SQL Server 2005 features.For more details on the differences between MDAC and SQL Native Client, read this msdn article >>AccessODBCStandard Security:"Driver={Microsoft Access Driver (*.mdb)};Dbq=C:\mydatabase.mdb;Uid=Admin;Pwd=;"Workgroup:"Driver={Microsoft Access Driver (*.mdb)};Dbq=C:\mydatabase.mdb;SystemDB=C:\mydatabase.m dw;"Exclusive:"Driver={Microsoft Access Driver (*.mdb)};Dbq=C:\mydatabase.mdb;Exclusive=1;Uid=admin;Pwd ="OLE DB, OleDbConnection (.NET)Standard security:"Provider=Microsoft.Jet.OLEDB.4.0;DataSource=\somepath\mydb.mdb;User Id=admin;Password=;"Workgroup (system database):"Provider=Microsoft.Jet.OLEDB.4.0;DataSource=\somepath\mydb.mdb;Jet OLEDB:System Database=system.mdw;"With password:"Provider=Microsoft.Jet.OLEDB.4.0;DataSource=\somepath\mydb.mdb;Jet OLEDB:Database Password=MyDbPassword;"OracleODBCNew version:"Driver={Microsoft ODBC for Oracle};Server=OracleServer.world;Uid=Username;Pwd=asdasd; "Old version:"Driver={Microsoft ODBC Driver for Oracle};ConnectString=OracleServer.world;Uid=myUsername;Pw d=myPassword;"OLE DB, OleDbConnection (.NET)Standard security:"Provider=msdaora;Data Source=MyOracleDB;User Id=UserName;Password=asdasd;"This one's from Microsoft, the following are from OracleStandard Security:"Provider=OraOLEDB.Oracle;Data Source=MyOracleDB;User Id=Username;Password=asdasd;"Trusted Connection:"Provider=OraOLEDB.Oracle;DataSource=MyOracleDB;OSAuthent=1;"OracleConnection (.NET)Standard:"Data Source=MyOracleDB;Integrated Security=yes;"This one works only with Oracle 8i release 3 or laterSpecifying username and password:"Data Source=MyOracleDB;User Id=username;Password=passwd;Integrated Security=no;"This one works only with Oracle 8i release 3 or laterDeclare the OracleConnection:C#:using System.Data.OracleClient;OracleConnection oOracleConn = new OracleConnection();oOracleConn.ConnectionString = "my connection string";oOracleConn.Open();:Imports System.Data.OracleClientDim oOracleConn As OracleConnection = New OracleConnection()oOracleConn.ConnectionString = "my connection string"oOracleConn.Open()Missing the System.Data.OracleClient namespace? Download .NET Managed Provider for Oracle >>Great article! "Features of Oracle Data Provider for .NET" by Rama Mohan G. at C# CornerCore Labs OraDirect (.NET)Standard:"User ID=scott; Password=tiger; Host=ora; Pooling=true; Min Pool Size=0;Max Pool Size=100; Connection Lifetime=0"Read more at Core Lab and the product page.Data ShapeMS Data Shape:"Provider=MSDataShape.1;Persist Security Info=False;Data Provider=MSDAORA;Data Source=orac;user id=username;password=mypw"Want to learn data shaping? Check out 4GuyfFromRolla's great article about Data Shaping >>MySQLMyODBCMyODBC 2.50 Local database:"Driver={mySQL};Server=localhost;Option=16834;Database =mydatabase;"MyODBC 2.50 Remote database:"Driver={mySQL};Server=;Port=3306;Optio n=131072;Stmt=;Database=my-database;Uid=username;Pwd=password;"MyODBC 3.51 Local database:"DRIVER={MySQL ODBC 3.51 Driver};SERVER=localhost;DATABASE=myDatabase;USER=myUs ername;PASSWORD=myPassword;OPTION=3;"MyODBC 3.51 Remote database:"DRIVER={MySQL ODBC 3.51 Driver};SERVER=;PORT=3306;DATABASE=myD atabase;USER=myUsername;PASSWORD=myPassword;OPTION=3;"OLE DB, OleDbConnection (.NET)Standard:"Provider=MySQLProv;Data Source=mydb;User Id=UserName;Password=asdasd;"Connector/Net 1.0 (.NET)Standard:"Server=Server;Database=Test;Uid=UserName;Pwd=asdasd ;"Download the driver at MySQL Developer Zone.Specifying port:"Server=Server;Port=1234;Database=Test;Uid=UserName;P wd=asdasd;"Default port is 3306. Enter value -1 to use a named pipe connection.Declare the MySqlClient connection:C#:using MySql.Data.MySqlClient;MySqlConnection oMySqlConn = new MySqlConnection();oMySqlConn.ConnectionString = "Server=Server;Database=Test;Uid=UserName;Pwd=asdasd;";oMySqlConn.Open();:Imports MySql.Data.MySqlClientDim oMySqlConn As MySqlConnection = New MySqlConnection()oMySqlConn.ConnectionString = "Server=Server;Database=Test;Uid=UserName;Pwd=asdasd;"oMySqlConn.Open()MySqlConnection (.NET)eInfoDesigns.dbProvider:"Data Source=server;Database=mydb;User ID=username;Password=pwd;Command Logging=false"This one is used with eInfoDesigns dbProvider, an add-on to .NETDeclare the MySqlConnection:C#:using eInfoDesigns.dbProvider.MySqlClient;MySqlConnection oMySqlConn = new MySqlConnection();oMySqlConn.ConnectionString = "my connection string";oMySqlConn.Open();:Imports eInfoDesigns.dbProvider.MySqlClientDim oMySqlConn As MySqlConnection = New MySqlConnection()oMySqlConn.ConnectionString = "my connection string"oMySqlConn.Open()SevenObjects MySqlClient (.NET)Standard:"Host=server; UserName=myusername; Password=mypassword;Database=mydb;"This is a freeware data provider from SevenObjectsCore Labs MySQLDirect (.NET)Standard:"User ID=root; Password=pwd; Host=localhost; Port=3306; Database=test;Direct=true; Protocol=TCP; Compress=false; Pooling=true; Min Pool Size=0;Max Pool Size=100; Connection Lifetime=0"Read more at Core Lab and the product page.InterbaseODBC, EasysoftLocal computer:"Driver={Easysoft IB6 ODBC};Server=localhost;Database=localhost:C:\mydatabase.gd b;Uid=username;Pwd=password"Remote Computer:"Driver={Easysoft IB6 ODBC};Server=ComputerName;Database=ComputerName:C:\m ydatabase.gdb;Uid=username;Pwd=password"Read more about this driver: Easysoft ODBC-Interbase driver >>ODBC, IntersolvLocal computer:"Driver={INTERSOLV InterBase ODBC Driver (*.gdb)};Server=localhost;Database=localhost:C:\mydatabase.gd b;Uid=username;Pwd=password"Remote Computer:"Driver={INTERSOLV InterBase ODBC Driver (*.gdb)};Server=ComputerName;Database=ComputerName:C:\ mydatabase.gdb;Uid=username;Pwd=password"This driver are provided by DataDirect Technologies >> (formerly Intersolv)OLE DB, SIBPROviderStandard:"provider=sibprovider;location=localhost:;datasource=c:\databases\gdbs\mygdb.gdb;userid=SYSDBA;password=masterkey"Specifying character set:"provider=sibprovider;location=localhost:;datasource=c:\databases\gdbs\mygdb.gdb;userid=SYSDBA;password=masterkey;character set=ISO8859_1"Specifying role:"provider=sibprovider;location=localhost:;datasource=c:\databases\gdbs\mygdb.gdb;userid=SYSDBA;password=masterkey;role=DIGITADORES"Read more about SIBPROvider >>Read more about connecting to Interbase in this Borland Developer Network articleIBM DB2OLE DB, OleDbConnection (.NET) from msTCP/IP:"Provider=DB2OLEDB;Network TransportLibrary=TCPIP;Network Address=XXX.XXX.XXX.XXX;Initial Catalog=MyCtlg;Package Collection=MyPkgCol;Default Schema=Schema;User ID=MyUser;Password=MyPW"APPC:"Provider=DB2OLEDB;APPC Local LU Alias=MyAlias;APPC Remote LU Alias=MyRemote;Initial Catalog=MyCtlg;Package Collection=MyPkgCol;Default Schema=Schema;User ID=MyUser;Password=MyPW"IBM's OLE DB Provider (shipped with IBM DB2 UDB v7 or above)TCP/IP:Provider=IBMDADB2;Database=sample;HOSTNAME=db2h ost;PROTOCOL=TCPIP;PORT=50000;uid=myUserName;pwd=my Pwd;ODBCStandard:"driver={IBM DB2 ODBC DRIVER};Database=myDbName;hostname=myServerName;port =myPortNum;protocol=TCPIP; uid=myUserName; pwd=myPwd"SybaseODBCStandard Sybase System 12 (or 12.5) Enterprise Open Client:"Driver={SYBASE ASE ODBC Driver};Srvr=Aron1;Uid=username;Pwd=password"Standard Sybase System 11:"Driver={SYBASE SYSTEM 11};Srvr=Aron1;Uid=username;Pwd=password;Database=mydb "For more information check out the Adaptive ServerEnterprise Document SetsIntersolv 3.10:"Driver={INTERSOLV 3.10 32-BIT Sybase};Srvr=Aron1;Uid=username;Pwd=password;"Sybase SQL Anywhere (former Watcom SQL ODBC driver):"ODBC; Driver=Sybase SQL Anywhere 5.0; DefaultDir=c:\dbfolder\;Dbf=c:\mydatabase.db;Uid=username;P wd=password;Dsn="""""Note! The two double quota following the DSN parameter at the end are escaped quotas (VB syntax), you may have to change this to your language specific escape syntax. The empty DSN parameter is indeed critical as not including it will result in error 7778.Read more in the Sybase SQL Anywhere User Guide (see part 3, chapter 13) >>OLE DBAdaptive Server Anywhere (ASA):"Provider=ASAProv;Data source=myASA"Read more in the ASA User Guide (part 1, chapter 2) >>Adaptive Server Enterprise (ASE) with Data Source .IDS file:"Provider=Sybase ASE OLE DB Provider; Data source=myASE"Note that you must create a Data Source .IDS file using the Sybase Data Administrator. These .IDS files resemble ODBC DSNs.Adaptive Server Enterprise (ASE):"Provider=Sybase.ASEOLEDBProvider;Srvr=myASEserver,50 00;Catalog=myDBname;UserId=username;Password=password"- some reports on problem using the above one, try the following as an alternative -"Provider=Sybase.ASEOLEDBProvider;ServerName=myASEserver,5000;Initial Catalog=myDBname;User Id=username;Password=password"This one works only from Open Client 12.5 where the server port number feature works,燼llowing fully qualified connection strings to be used without defining燼ny .IDS Data Source files.AseConnection (.NET)Standard:"DataSource='myASEserver';Port=5000;Database='myDBname';UID=' username';PWD='password';"Declare the AseConnection:C#:using Sybase.Data.AseClient;AseConnection oCon = new AseConnection();oCon.ConnectionString="my connection string";oCon.Open();:Imports System.Data.AseClientDim oCon As AseConnection = New AseConnection()oCon.ConnectionString="my connection string"oCon.Open()Read more! Adaptive Server Enterprise Data Provider Documentation >>InformixODBCInformix 3.30:"Dsn='';Driver={INFORMIX 3.30 32 BIT};Host=hostname;Server=myserver;Service=service-name;Protocol=olsoctcp;Database=mydb;UID=username;PWD= myPwdInformix-CLI 2.5:"Driver={Informix-CLI 2.5 (32Bit)};Server=myserver;Database=mydb;Uid=username;Pwd=my Pwd"OLE DBIBM Informix OLE DB Provider:"Provider=Ifxoledbc.2;password=myPw;UserID=myUser;Data Source=dbName@serverName;Persist Security Info=true"IngresODBCDSN-less"Provider=MSDASQL.1;DRIVER=Ingres;SRVR=xxxxx;DB=xxx xx;Persist Security Info=False;uid=xxxx;pwd=xxxxx;SELECTLOOPS=N;Extended Properties="""SERVER=xxxxx;DATABASE=xxxxx;SERVERTYPE=IN GRES""Mimer SQLODBCStandard Security:"Driver={MIMER};Database=mydb;Uid=myuser;Pwd=mypw; "Prompt for username and password:"Driver={MIMER};Database=mydb;"LightbaseStandardStandard:"user=USERLOGIN;password=PASSWORD;UDB=USERBASE; server=SERVERNAME"PostgreSQLCore Labs PostgreSQLDirect (.NET)Standard:"User ID=root; Password=pwd; Host=localhost; Port=5432; Database=testdb;Pooling=true; Min Pool Size=0; Max Pool Size=100; Connection Lifetime=0"Read more at Core Lab and the product page.PostgreSQL driverStandard:"DRIVER={PostgreSQL};SERVER=ipaddress;port=5432;DATA BASE=dbname;UID=username;PWD=password;"Npgsql by pgFoundry (.NET)SSL activated:"Server=127.0.0.1;Port=5432;Userid=myuserid;password=m ypw;Protocol=3;SSL=true;Pooling=true;MinPoolSize=3;MaxPool Size=20;Encoding=UNICODE;Timeout=20;SslMode=Require"Without SSL:"Server=127.0.0.1;Port=5432;Userid=myuserid;password=m ypw;Protocol=3;SSL=false;Pooling=true;MinPoolSize=1;MaxPoo lSize=20;Encoding=UNICODE;Timeout=15;SslMode=Disable"Read more in the Npgsql: User's Manual and on the pgFoundry website.ParadoxODBC5.X:Driver={Microsoft Paradox Driver (*.db )};DriverID=538;Fil=Paradox 5.X;DefaultDir=c:\pathToDb\;Dbq=c:\pathToDb\;CollatingSeque nce=ASCII"7.X:"Provider=MSDASQL.1;Persist Security Info=False;Mode=Read;ExtendedProperties='DSN=Paradox;DBQ=C:\myDb;DefaultDir=C:\myDb; DriverId=538;FIL=Paradox7.X;MaxBufferSize=2048;PageTimeout=600;';InitialCatalog=C:\myDb"OleDbConnection (.NET)Standard"Provider=Microsoft.Jet.OLEDB.4.0;DataSource=c:\myDb;Extended Properties=Paradox 5.x;"。
数据库的连接串在数据库的各种应用程序开发中,连接数据库是数据库应用程序开发的第一步,同时也是最重要的一步。
而对于不同的数据库他们的连接模式各有不同,对应的连接串也不同。
程序员可能都有这样的经历,有时不知道连接数据库所需要的连接串究竟如何写或者经常写错而导致不能正确访问数据库。
当然很多编程工具能够通过可视化的界面直接产生正确的连接字符串,但字符串中各个参数的具体含义也不清楚,经常混淆。
本文就针对大部分常用数据库列举出不同连接方法所需要的连接字符串并加以说明,以便程序员参考!Sql Server∙ ODBCo标准连接(Standard Security):"Driver={SQLServer};Server=Aron1;Database=pubs;Uid=sa;Pwd=asdasd;"1)当服务器为本地时Server可以使用(local);"Driver={SQLServer};Server=(local);Database=pubs;Uid=sa;Pwd=asdasd;"2)当连接远程服务器时,需指定地址、端口号和网络库"Driver={SQLServer};Server=130.120.110.001;Address=130.120.110.001,1052;Network=dbmssocn;Database=pubs;Uid=sa;Pwd=asdasd;"注:Address参数必须为IP地址,而且必须包括端口号o信任连接(Trusted connection): (Microsoft Windows NT 集成了安全性)"Driver={SQLServer};Server=Aron1;Database=pubs;Trusted_Connection=yes;"或者"Driver={SQL Server};Server=Aron1;Database=pubs; Uid=;Pwd=;"o连接时弹出输入用户名和口令对话框:Conn.Properties("Prompt") = adPromptAlwaysConn.Open "Driver={SQL Server};Server=Aron1;DataBase=pubs;"∙ OLE DB, OleDbConnection (.NET)o标准连接(Standard Security):"Provider=sqloledb;Data Source=Aron1;Initial Catalog=pubs;UserId=sa;Password=asdasd;"o信任连接(Trusted connection):"Provider=sqloledb;Data Source=Aron1;Initial Catalog=pubs;IntegratedSecurity=SSPI;"(如果连接一个具体的已命名SQLServer实例,使用DataSource=Servere Name\Instance Name;但仅适用于SQLServer2000)例如:”Provider=sqloledb;DataSource=MyServerName\MyInstanceName;InitialCatalog=MyDatabaseName;UserId=MyUsername;Password=MyPassword;”o连接时弹出输入用户名和口令对话框:Conn.Provider = "sqloledb"Conn.Properties("Prompt") = adPromptAlwaysConn.Open "Data Source=Aron1;Initial Catalog=pubs;"o通过IP地址连接:"Provider=sqloledb;Data Source=190.190.200.100,1433;NetworkLibrary=DBMSSOCN;Initial Catalog=pubs;User ID=sa;Password=asdasd;"(DBMSSOCN=TCP/IP代替Named Pipes, Data Source的末尾是需要使用的端口号(缺省为1433))SqlConnection (.NET)o标准连接(Standard Security):"Data Source=Aron1;Initial Catalog=pubs;User Id=sa;Password=asdasd;"或者"Server=Aron1;Database=pubs;UserID=sa;Password=asdasd;Trusted_Connection=False"(这两个连接串的结果相同)o信任连接(Trusted connection):"Data Source=Aron1;Initial Catalog=pubs;Integrated Security=SSPI;"或者"Server=Aron1;Database=pubs;Trusted_Connection=True;"(这两个连接串的结果相同)(可以用serverName\instanceName代替Data Source,取值为一个具体的SQLServer实例,但仅适用于SQLServer2000)o通过IP地址连接:"Data Source=190.190.200.100,1433;Network Library=DBMSSOCN;InitialCatalog=pubs;User ID=sa;Password=asdasd;"(DBMSSOCN=TCP/IP代替Named Pipes, Data Source的末尾是需要使用的端口号(缺省为1433))o SqlConnection连接的声明:C#:using System.Data.SqlClient;SqlConnection SQLConn = new SqlConnection();SQLConn.ConnectionString="my connectionstring";SQLConn.Open();:Imports System.Data.SqlClientDim SQLConn As SqlConnection = New SqlConnection()SQLConn.ConnectionString="my connectionstring"SQLConn.Open()∙ Data Shapeo MS Data Shape"Provider=MSDataShape;Data Provider=SQLOLEDB;DataSource=Aron1;Initial Catalog=pubs;User ID=sa;Password=asdasd;"∙更多o如何定义使用哪个协议▪举例:"Provider=sqloledb;Data Source=190.190.200.100,1433;NetworkLibrary=DBMSSOCN;Initial Catalog=pubs;UserID=sa;Password=asdasd;"名称网络协议库dbnmpntw Win32 Named Pipesdbmssocn Win32 Winsock TCP/IPdbmsspxn Win32 SPX/IPXdbmsvinn Win32 Banyan Vinesdbmsrpcn Win32 Multi-Protocol (Windows RPC)▪重要提示当通过SQLOLEDB提供者进行连接时使用以下语法:Network Library=dbmssocn但通过MSDASQL提供者进行连接时使用以下语法:Network=dbmssocno所有SqlConnection连接串属性▪下表显示了 SqlConnection对象的所有连接串属性. 其中大多数的属性也在ADO中使用.所有属性和描述来自于msdn.注意:使用分号分隔每个属性如果一个名字出现多于两次,在连接串中的最后一次出现的值将被使用。
mysql连接字符串的函数MySQL连接字符串是指用于连接MySQL数据库的字符串,其格式为:mysql://user:password@host:port/database。
在使用MySQL连接字符串时,通常需要使用一些函数来构造和处理它。
下面介绍几个常用的MySQL连接字符串函数:1. mysql_connect()函数:用于连接MySQL数据库。
它的参数包括数据库服务器的地址、用户名、密码和要连接的数据库名。
例如: $link = mysql_connect('localhost', 'root', 'password');2. mysql_select_db()函数:用于选择要连接的数据库。
它的参数是所选数据库的名称。
例如:mysql_select_db('test_db');3. mysql_query()函数:用于执行SQL查询语句。
它的参数是要执行的查询语句。
例如:$result = mysql_query('SELECT * FROM customers');4. mysql_fetch_array()函数:用于从查询结果集中获取数据。
它的参数是一个查询结果集,返回值是一个包含查询结果的数组。
例如:while ($row = mysql_fetch_array($result)) {echo $row['name'] . ' - ' . $row['email'];}5. mysql_close()函数:用于关闭数据库连接。
它不需要参数。
例如:mysql_close();以上就是一些常用的MySQL连接字符串函数。
使用它们可以方便地构造和处理MySQL连接字符串,从而实现与MySQL数据库的连接和操作。
SQL数据库连接字符串的⼏种写法整理⼀、远程连接1.sql server ⾝份验证连接字符串:private string ConnstrSqlServer = "server=服务器名称;uid=登录名称;pwd=登录密码;database=数据库名称";2.windows ⾝份验证连接字符串:private string ConnstrWindows = "server=服务器名称;database=数据库名称;Trusted_Connection=SSPI";⼆、本地连接. 在 C# 代码中⽤SqlClient的⽅式访问 SQL Server 2008-2014 数据库 Framework Data Provider for SQL Server 标准写法 Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;2. .NET Framework Data Provider for SQL Server 另⼀种标准写法 Server=myServerAddress;Database=myDataBase;User ID=myUsername;Password=myPassword;Trusted_Connection=False; Framework Data Provider for SQL Server 信任连接写法 Data Source=myServerAddress;Initial Catalog=myDataBase;Integrated Security=SSPI; Framework Data Provider for SQL Server 信任连接另⼀种写法 Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;。
数据库链接字符串⼤全IBM .Net Data Provider您需要使⽤ IBM.Data.DB2.iSeries 命名空间DataSource=myServerAddress;UserID=myUsername; Password=myPassword;DataCompression=True;OLE DB, OleDbConnection (.NET)IBM Client Access OLE DB 适配器Provider=IBMDA400;Data Source=MY_SYSTEM_NAME;User Id=myUsername; Password=myPassword;MY_SYSTEM_NAME是在OperationsNavigator中的系统连接名称。
IBM Client Access OLE DB 适配器Provider=IBMDA400;Data Source=MY_SYSTEM_NAME;User Id=myUsername; Password=myPassword;DefaultCollection=MY_LIBRARY;MY_SYSTEM_NAME是系统连接的名称,MY_LIBRARY是iSeries Navigator中library的名称。
ODBCIBM Client Access ODBC 驱动Driver={Client Access ODBC Driver (32-bit)};System=my_system_name; Uid=myUsername; Pwd=myPassword;IBM iSeries Access ODBC 驱动这个驱动需要⽐IBM Client Access ODBC的驱动更新。
Driver={iSeries Access ODBC Driver};System=my_system_name; Uid=myUsername; Pwd=myPassword;CachéODBC标准DRIVER={InterSystems ODBC};SERVER=myServerAddress; DATABASE=myDataBase; UID=myUsername;PWD=myPassword; 特定端⼝DRIVER={InterSystems ODBC};SERVER=myServerAddress;PORT=12345; DATABASE=myDataBase;UID=myUsername;PWD=myPassword;特定协议DRIVER={InterSystems ODBC};SERVER=myServerAddress;PORT=12345; DATABASE=myDataBase; PROTOCOL=TCP;STATIC CURSORS=1;UID=myUsername;PWD=myPassword;IBM DB2OLE DB, OleDbConnection (.NET)TCP/IPProvider=DB2OLEDB;Network Transport Library=TCPIP;Network Address=XXX.XXX.XXX.XXX; Initial Catalog=MyCtlg;Package Collection=MyPkgCol;Default Schema=Schema;User ID=myUsername; Password=myPassword;APPCProvider=DB2OLEDB;APPC Local LU Alias=MyAlias;APPC Remote LU Alias=MyRemote; Initial Catalog=MyCtlg;Package Collection=MyPkgCol; Default Schema=Schema;User ID=myUsername;Password=myPassword;IBM提供的OLE DB适配器TCP/IPProvider=IBMDADB2;Database=myDataBase; Hostname=myServerAddress; Protocol=TCPIP;Port=50000;Uid=myUsername;Pwd=myPassword;ODBC标准Driver={IBM DB2 ODBC DRIVER}; Database=myDataBase;Hostname=myServerAddress;Port=1234;Protocol=TCPIP;Uid=myUsername;Pwd=myPassword;FirebirdODBC - IBPhoenix Open Source标准Driver=Firebird/InterBase(r) driver;Uid=SYSDBA; Pwd=masterkey; DbName=D:\FIREBIRD\examples\TEST.FDB;.NET - Firebird .Net Data Provider标准User=SYSDBA;Password=masterkey;Database=SampleDatabase.fdb; DataSource=localhost;Port=3050;Dialect=3;Charset=NONE;Role=; Connection lifetime=15;Pooling=true;MinPoolSize=0;MaxPoolSize=50;Packet Size=8192;ServerType=0; InformixODBCInformix 3.30Dsn=;Driver={INFORMIX 3.30 32 BIT};Host=hostname;Server=myServerAddress; Service=service-name; Protocol=olsoctcp; Database=myDataBase;Uid=myUsername;Pwd=myPassword;Informix-CLI 2.5:Driver={Informix-CLI 2.5 (32 Bit)};Server=myServerAddress; Database=myDataBase; Uid=myUsername;Pwd=myPassword;OLE DBIBM Informix OLE DB ProviderProvider=Ifxoledbc.2;Password=myPassword; User ID=myUsername; Data Source=dbName@serverName;Persist Security Info=true; IBM Informix .NET Providerrmix.IfxConnection(命名空间)Database=myDataBase;Host=192.168.10.10; Server=db_engine_tcp; Service=1492;Protocol=onsoctcp;UID=myUsername;Password=myPassword;IngresODBCDSN-lessProvider=MSDASQL.1;DRIVER=Ingres;SRVR=xxxxx;DB=xxxxx;Persist SecurityInfo=False;Uid=myUsername;Pwd=myPassword; SELECTLOOPS=N; ExtendedProperties="SERVER=xxxxx;DATABASE=xxxxx;SERVERTYPE=INGRES";InterbaseODBC, Easysoft本地计算机Driver={Easysoft IB6 ODBC}; Server=localhost ;Database=localhost:C:\mydatabase.gdb; Uid=myUsername; Pwd=myPassword;远程计算机Driver={Easysoft IB6 ODBC}; Server=myServerAddress; Database=ComputerName: C:\mydatabase.gdb;Uid=myUsername;Pwd=myPassword;ODBC, Intersolv本地计算机Driver={INTERSOLV InterBase ODBC Driver (*.gdb)}; Server=localhost; Database=localhost: C:\mydatabase.gdb; Uid=myUsername; Pwd=myPassword;远程计算机Driver={INTERSOLV InterBase ODBC Driver (*.gdb)}; Server=myServerAddress; Database=ComputerName:C:\mydatabase.gdb;Uid=myUsername; Pwd=myPassword;OLE DB, SIBPROvider标准provider=sibprovider;location=localhost:;data source=c:\databases\gdbs\mygdb.gdb; user id=SYSDBA; Password=masterkey;版本 2.xProvider=SIBPROvider.2; Data Source=localhost:c:\databases\gdbs\mygdb.gdb; Persist Security Info=False;指定字符集provider=sibprovider;location=localhost:;data source=c:\databases\gdbs\mygdb.gdb; user id=SYSDBA; Password=masterkey; character set=ISO8859_1;指定⾓⾊provider=sibprovider;location=localhost:; data source=c:\databases\gdbs\mygdb.gdb;user id=SYSDBA; Password=masterkey;role=DIGITADORES;Lightbase标准连接标准User=myUsername;Password=myPassword;UDB=USERBASE;Server=myServerAddress;MySQLMyODBCMyODBC 2.50 本地数据库Driver={mySQL};Server=localhost;Option=16834;Database=myDataBase;MyODBC 2.50 远程数据库Driver={mySQL}; Server=myServerAddress;Port=3306; Option=131072;Stmt=; Database=myDataBase; Uid=myUsername;Pwd=myPassword;MyODBC 3.51 本地数据库Driver={MySQL ODBC 3.51 Driver};Server=localhost;Database=myDataBase; User=myUsername;Password=myPassword;Option=3; MyODBC 3.51 远程数据库Driver={MySQL ODBC 3.51 Driver};Server=;Port=3306; Database=myDataBase; User=myUsername;Password=myPassword;Option=3;OLE DB, OleDbConnection (.NET)标准Provider=MySQLProv; Data Source=mydb; User Id=myUsername; Password=myPassword;Connector/Net 1.0 (.NET)标准Server=myServerAddress; Database=myDataBase; Uid=myUsername;Pwd=myPassword;默认端⼝号是3306指定端⼝号Server=myServerAddress; Port=1234;Database=myDataBase; Uid=myUsername; Pwd=myPassword;命名管道Server=myServerAddress; Port=-1;Database=myDataBase; Uid=myUsername;Pwd=myPassword;如果端⼝是-1,意思是告诉驱动程序使⽤命名管道⽹络协议来连接数据库。
OleDb连接各种数据库的字符串Access数据库Provider=Microsoft.Jet.OLEDB.4.0; Data Source=D:\test.mdbSql数据库Provider=SQLOLEDB; Data Source=服务器名称; Initial Catalog=数据库名称; User ID=登录名; Password='登录密码'eg:Provider=SQLOLEDB; Data Source=192.168.0.100\SQL2008; Initial Catalog=wuhan_gl; User ID=sa; Password='sa'Oracle数据库(必须安装Oracle客户端)Provider=OraOLEDB.Oracle;Data Source=服务器名称;User Id=登录名;Password=密码eg:Provider=OraOLEDB.Oracle;Data Source=orcl;User Id=wuhan_gl;Password=123456另:OracleClient远程连接Oracle数据库的字符串SERVER=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=IP地址)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=服务器名称)));uid=登录名;pwd=登录密码; ps:1521为端口号eg:SERVER=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=1192.168.0.100)(POR T=1521))(CONNECT_DA TA=(SERVICE_NAME=orcl)));uid=wuhan_gl;pwd=123456;SqlClient远程连接SQL数据库Server=服务器IP; Data Source=服务器名称; Initial Catalog=数据库名称; user id=登录名; password='登录密码'eg:Server=192.168.0.100; Data Source=192.168.0.100\SQL2008; Initial Catalog=wuhan_gl; user id=sa; password='sa'。
数据库的连接串在数据库的各种应用程序开发中,连接数据库是数据库应用程序开发的第一步,同时也是最重要的一步。
而对于不同的数据库他们的连接模式各有不同,对应的连接串也不同。
程序员可能都有这样的经历,有时不知道连接数据库所需要的连接串究竟如何写或者经常写错而导致不能正确访问数据库。
当然很多编程工具能够通过可视化的界面直接产生正确的连接字符串,但字符串中各个参数的具体含义也不清楚,经常混淆。
本文就针对大部分常用数据库列举出不同连接方法所需要的连接字符串并加以说明,以便程序员参考!Sql Server· ODBCo 标准连接(Standard Security):"Driver={SQL Server};Server=Aron1;Database=pubs;Uid=sa;Pwd=asdasd;" 1)当服务器为本地时Server可以使用(local);"Driver={SQLServer};Server=(local);Database=pubs;Uid=sa;Pwd=asdasd;"2)当连接远程服务器时,需指定地址、端口号和网络库"Driver={SQLServer};Server=130.120.110.001;Address=130.120.110.001,1052;Network= dbmssocn;Database=pubs;Uid=sa;Pwd=asdasd;"注:Address参数必须为IP地址,而且必须包括端口号o 信任连接(Trusted connection): (Microsoft Windows NT 集成了安全性)"Driver={SQLServer};Server=Aron1;Database=pubs;Trusted_Connection=yes;"或者"Driver={SQL Server};Server=Aron1;Database=pubs; Uid=;Pwd=;"o 连接时弹出输入用户名和口令对话框:Conn.Properties("Prompt") = adPromptAlwaysConn.Open "Driver={SQL Server};Server=Aron1;DataBase=pubs;"· OLE DB, OleDbConnection (.NET)o 标准连接(Standard Security):"Provider=sqloledb;Data Source=Aron1;Initial Catalog=pubs;UserId=sa;Password=asdasd;"o 信任连接(Trusted connection):"Provider=sqloledb;Data Source=Aron1;Initial Catalog=pubs;Integrated Security=SSPI;"(如果连接一个具体的已命名SQLServer实例,使用Data Source=Servere Name\Instance Name;但仅适用于SQLServer2000)例如:”Provider=sqloledb;DataSource=MyServerName\MyInstanceName;InitialCatalog=MyDatabaseName;UserId=MyUsername;Password=MyPassword;”o 连接时弹出输入用户名和口令对话框:Conn.Provider = "sqloledb"Conn.Properties("Prompt") = adPromptAlwaysConn.Open "Data Source=Aron1;Initial Catalog=pubs;"o 通过IP地址连接:"Provider=sqloledb;Data Source=190.190.200.100,1433;NetworkLibrary=DBMSSOCN;Initial Catalog=pubs;User ID=sa;Password=asdasd;" (DBMSSOCN=TCP/IP代替Named Pipes, Data Source的末尾是需要使用的端口号(缺省为1433))· SqlConnection (.NET)o 标准连接(Standard Security):"Data Source=Aron1;Initial Catalog=pubs;User Id=sa;Password=asdasd;" 或者"Server=Aron1;Database=pubs;UserID=sa;Password=asdasd;Trusted_Connection=False"(这两个连接串的结果相同)o 信任连接(Trusted connection):"Data Source=Aron1;Initial Catalog=pubs;Integrated Security=SSPI;"或者"Server=Aron1;Database=pubs;Trusted_Connection=True;"(这两个连接串的结果相同)(可以用serverName\instanceName代替Data Source,取值为一个具体的SQLServer实例,但仅适用于SQLServer2000)o 通过IP地址连接:"Data Source=190.190.200.100,1433;Network Library=DBMSSOCN;Initial Catalog=pubs;User ID=sa;Password=asdasd;"(DBMSSOCN=TCP/IP代替Named Pipes, Data Source的末尾是需要使用的端口号(缺省为1433))o SqlConnection连接的声明:C#:using System.Data.SqlClient;SqlConnection SQLConn = new SqlConnection();SQLConn.ConnectionString="my connectionstring";SQLConn.Open();:Imports System.Data.SqlClientDim SQLConn As SqlConnection = New SqlConnection()SQLConn.ConnectionString="my connectionstring"SQLConn.Open()· Data Shapeo MS Data Shape"Provider=MSDataShape;Data Provider=SQLOLEDB;DataSource=Aron1;Initial Catalog=pubs;User ID=sa;Password=asdasd;"·更多o 如何定义使用哪个协议§举例:"Provider=sqloledb;Data Source=190.190.200.100,1433;NetworkLibrary=DBMSSOCN;Initial Catalog=pubs;User ID=sa;Password=asdasd;" 名称网络协议库dbnmpntw Win32 Named Pipesdbmssocn Win32 Winsock TCP/IPdbmsspxn Win32 SPX/IPXdbmsvinn Win32 Banyan Vinesdbmsrpcn Win32 Multi-Protocol (Windows RPC)§重要提示当通过SQLOLEDB提供者进行连接时使用以下语法:Network Library=dbmssocn但通过MSDASQL提供者进行连接时使用以下语法:Network=dbmssocno 所有SqlConnection连接串属性§下表显示了 SqlConnection对象的所有连接串属性. 其中大多数的属性也在ADO中使用.所有属性和描述来自于msdn.名称缺省值描述Application Name 应用程序名称或者当没有提供应用程序时为.Net SqlClient数据提供者AttachDBFilename或者extended properties或者Initial File Name 主要文件的名字,包括相关联数据库的全路径。
数据库名字必须通过关键字''database''来指定。
Connect Timeout或者Connection Timeout 15 在中止连接请求,产生错误之前等待服务器连接的时间(以秒为单位)Connection Lifetime 0 当一个连接返回到连接池,当前时间与连接创建时间的差值,如果时间段超过了指定的连接生存时间,此连接就被破坏。
它用于聚集设置中在运行服务器和准备上线的服务器之间强制负载平衡。
Connection Reset ''true'' 当连接从连接池移走时决定是否重置数据库连接。
当设置为''false''时用于避免获得连接时的额外服务器往复代价。
Current Language SQL Server语言记录名称Data Source或Server或Address或Addr或Network Address 要连接的SQL Server实例的名字或者网络地址Enlist ''true'' 为真时,连接池自动列出创建线程的当前事务上下文中的连接。
Initial Catalog或Database 数据库名Integrated Security或者Trusted_Connection ''false'' 连接是否为信任连接。
其取值为''true'',''false''和''sspi''(等于''true'').Max Pool Size 100 连接池中允许的最大连接数Min Pool Size 0 连接池中允许的最小连接数Network Library或Net ''dbmssocn'' 网络库用于建立与一个SQL Server 实例的连接。