【dotNET学习笔记】2009-12-10 主题:SQL数据库连接字符串
2009-12-11 10:41
429 查看
dotNET学习笔记
2009-12-10
主题:SQL数据库连接字符串
1 SQL数据库连接字符串的常用属性
以下引用的表格来自:
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectionstring.aspx
准确的说下面这些属性是我现在常用到的,其它的指定连接池大小以后也应该会用到。
1.1 Data Source或Server或Address或Addr或Network Address
描述:
用于连接的SQL服务器实例的网络地址,服务器名称后面可接端口号。
指示本地实例的时候使用(local)。强制性的加上协议可以在地址前加前缀。
1.2 Initial Catalog或Database
描述:
数据库名称。
1.3 Integrated Security或Trusted_Connection
描述:
设为false则需要在连接时指明用户名和密码。设为true,则使用当前Windows账户进行验证。
可以识别的值为true、false、yes、no,和sspi(强烈推荐使用,跟true效果相同)。
1.4 Persist Security Info
描述:
当设为false和no(强烈推荐使用)时,一些安全敏感信息,比如密码,在连接打开时将作为连接的一部分被返回。重置连接字符串将会重置连接字符串相关的一切值,包括密码。可以识别的值为true、false、yes和no。
1.5 User ID
描述:
SQL服务器的登录账户名,不推荐(前面讲Integrated Security或Trusted_Connection的时候推荐使用sspi从而不用指定账户名和密码)。
1.6 Password或Pwd
描述:
SQL服务器的登录账户密码,不推荐(前面讲Integrated Security或Trusted_Connection的时候推荐使用sspi从而不用指定账户名和密码)。
2 连接字符串实例
2.1 创建连接字符串
网站根目录下的有个名为Web.config的xml文件,在其中的<configuration>标签级别中加入<connectionStrings>标签,设定一个公用的数据库连接字符串:
<connectionStrings>
<add name="MyConnStr1" connectionString="Server=SSW; Database=Compass; Integrated Security=false; User ID=sa; Pwd=123456;"/>
<add name="MyConnStr2" connectionString="Data Source=127.0.0.1; Initial Catalog=test; Integrated Security=true;"/>
<add name=" " connectionString=" "/><!-- 还可以创建其它连接字符串 -->
</connectionStrings>
2.1.1 MyConnStr1
(1)name="MyConnStr1"
指定这个连接字符串的名称。
(2)Server=SSW;
指定连接的数据库实例名称。
(3)Database=Compass;
指定连接的数据库名称。
(4)Integrated Security=false;
不使用集成安全模式,而是指定用户名和密码。
(5)User ID=sa;
指定登录数据库服务器的登录名。
(6)Pwd=123456;
指定登录密码。安全起见密码应该加密,后面我会学习到的。
2.1.2 MyConnStr2
与MyConnStr1同理,只不过使用的是Windows身份验证,不需要指定用户名和密码。
2.2 引用连接字符串
在.cs文件中引用设定的连接字符串。
首先必须加入“using System.Configuration;”语句,然后:
string connStr = ConfigurationManager.ConnectionStrings["MyConnStr1"].ConnectionString.ToString();
ConnectionStrings["MyConnStr1"]定位到<connectionStrings>标签下名为“MyConnStr1”的标签,用.ConnectionString取其ConnectionString属性(标签有name和ConnectionString两个属性),最后用ToString方法将其转换为string对象。
3 SQLServer数据库相关设定(我用的是MS SQLServer 2008)
3.1 启用混合登录模式
刚才的第一个连接字符串使用用户名sa登录,这还需要在数据库中启用混合登录模式。
(1)右键单击服务器实例↓
(2)在“选择页”里选择“安全性”,在右边选择“SQL Server 和 Windows 身份验证模式”(即混合验证模式)↓
3.2 修改密码,开启账户
(1)在对象资源管理器中右键单击“安全性”下“登录名”中的“sa”↓
(2)打开sa的登陆属性窗口,在“常规”中将密码改掉(我忘记原始密码了,只能改掉)↓
(3)接着在“状态”中将“是否允许连接到数据库引擎”设为“授予”,将“登录”设为“启用”↓
这样就可以使用账户sa登录数据库了。
2009-12-10
主题:SQL数据库连接字符串
1 SQL数据库连接字符串的常用属性
以下引用的表格来自:
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectionstring.aspx
准确的说下面这些属性是我现在常用到的,其它的指定连接池大小以后也应该会用到。
1.1 Data Source或Server或Address或Addr或Network Address
Keyword 属性名称 | Default 默认值 | Description 描述 |
Data Source -or- Server -or- Address -or- Addr -or- Network Address | N/A | The name or network address of the instance of SQL Server to which to connect. The port number can be specified after the server name: server=tcp:servername, portnumber When specifying a local instance, always use (local). To force a protocol, add one of the following prefixes: np:(local), tcp:(local), lpc:(local) Note: ADO.NET 2.0 does not support asynchronous commands over shared memory for SQL Server 2000 or earlier. However, you can force the use of TCP instead of shared memory, either by prefixing tcp: to the server name in the connection string, or by using localhost. |
用于连接的SQL服务器实例的网络地址,服务器名称后面可接端口号。
指示本地实例的时候使用(local)。强制性的加上协议可以在地址前加前缀。
1.2 Initial Catalog或Database
Keyword | Default | Description |
Initial Catalog -or- Database | N/A | The name of the database. |
数据库名称。
1.3 Integrated Security或Trusted_Connection
Keyword | Default | Description |
Integrated Security -or- Trusted_Connection | 'false' | When false, User ID and Password are specified in the connection. When true, the current Windows account credentials are used for authentication. Recognized values are true, false, yes, no, and sspi (strongly recommended), which is equivalent to true. If User ID and Password are specified and Integrated Security is set to true, the User ID and Password will be ignored and Integrated Security will be used. |
设为false则需要在连接时指明用户名和密码。设为true,则使用当前Windows账户进行验证。
可以识别的值为true、false、yes、no,和sspi(强烈推荐使用,跟true效果相同)。
1.4 Persist Security Info
Keyword | Default | Description |
Persist Security Info | 'false' | When set to false or no (strongly recommended), 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. Recognized values are true, false, yes, and no. |
当设为false和no(强烈推荐使用)时,一些安全敏感信息,比如密码,在连接打开时将作为连接的一部分被返回。重置连接字符串将会重置连接字符串相关的一切值,包括密码。可以识别的值为true、false、yes和no。
1.5 User ID
Keyword | Default | Description |
User ID | N/A | The SQL Server login account. Not recommended. To maintain a high level of security, we strongly recommend that you use the Integrated Security or Trusted_Connection keywords instead. |
SQL服务器的登录账户名,不推荐(前面讲Integrated Security或Trusted_Connection的时候推荐使用sspi从而不用指定账户名和密码)。
1.6 Password或Pwd
Keyword | Default | Description |
Password -or- Pwd | N/A | The password for the SQL Server account logging on. Not recommended. To maintain a high level of security, we strongly recommend that you use the Integrated Security or Trusted_Connection keyword instead. |
SQL服务器的登录账户密码,不推荐(前面讲Integrated Security或Trusted_Connection的时候推荐使用sspi从而不用指定账户名和密码)。
2 连接字符串实例
2.1 创建连接字符串
网站根目录下的有个名为Web.config的xml文件,在其中的<configuration>标签级别中加入<connectionStrings>标签,设定一个公用的数据库连接字符串:
<connectionStrings>
<add name="MyConnStr1" connectionString="Server=SSW; Database=Compass; Integrated Security=false; User ID=sa; Pwd=123456;"/>
<add name="MyConnStr2" connectionString="Data Source=127.0.0.1; Initial Catalog=test; Integrated Security=true;"/>
<add name=" " connectionString=" "/><!-- 还可以创建其它连接字符串 -->
</connectionStrings>
2.1.1 MyConnStr1
(1)name="MyConnStr1"
指定这个连接字符串的名称。
(2)Server=SSW;
指定连接的数据库实例名称。
(3)Database=Compass;
指定连接的数据库名称。
(4)Integrated Security=false;
不使用集成安全模式,而是指定用户名和密码。
(5)User ID=sa;
指定登录数据库服务器的登录名。
(6)Pwd=123456;
指定登录密码。安全起见密码应该加密,后面我会学习到的。
2.1.2 MyConnStr2
与MyConnStr1同理,只不过使用的是Windows身份验证,不需要指定用户名和密码。
2.2 引用连接字符串
在.cs文件中引用设定的连接字符串。
首先必须加入“using System.Configuration;”语句,然后:
string connStr = ConfigurationManager.ConnectionStrings["MyConnStr1"].ConnectionString.ToString();
ConnectionStrings["MyConnStr1"]定位到<connectionStrings>标签下名为“MyConnStr1”的标签,用.ConnectionString取其ConnectionString属性(标签有name和ConnectionString两个属性),最后用ToString方法将其转换为string对象。
3 SQLServer数据库相关设定(我用的是MS SQLServer 2008)
3.1 启用混合登录模式
刚才的第一个连接字符串使用用户名sa登录,这还需要在数据库中启用混合登录模式。
(1)右键单击服务器实例↓
(2)在“选择页”里选择“安全性”,在右边选择“SQL Server 和 Windows 身份验证模式”(即混合验证模式)↓
3.2 修改密码,开启账户
(1)在对象资源管理器中右键单击“安全性”下“登录名”中的“sa”↓
(2)打开sa的登陆属性窗口,在“常规”中将密码改掉(我忘记原始密码了,只能改掉)↓
(3)接着在“状态”中将“是否允许连接到数据库引擎”设为“授予”,将“登录”设为“启用”↓
这样就可以使用账户sa登录数据库了。
相关文章推荐
- Python学习笔记——字符串连接
- Python学习笔记-- 字符串和数字的连接
- Python学习笔记之疑问10:如何使用分隔符连接list中的字符串
- DataBase 学习笔记一:c#连接SQL数据库
- [学习笔记]将LinqToSql的连接字符串写在配置文件中
- JavaScript 学习笔记(七)字符串的连接
- Effective Java 学习笔记(第51条:当心字符串的连接性能)
- 【学习笔记】〖ASP.NET〗连接字符串的函数
- Python学习笔记--Python字符串连接方法总结
- JavaScript学习笔记第一天——字符串连接的N种方法
- 算法第4版(谢路云译)学习笔记(3) -- 字符串连接操作的时间复杂度
- 0714学习笔记list常用操作索引(index)和切片(slice) 字符串的方法分割split和连接join
- JavaScript 学习笔记(七)字符串的连接
- python3.4学习笔记(十五) 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
- 【黑马程序员】连接字符串放在配置文件中方法(学习笔记)
- .Net连接字符串(ConnectionString)学习笔记(转)
- Java学习笔记(二十六):字符串连接用+和StringBuilder的append的区别
- 图片式笔记:两种验证模式下的 SQL数据库连接字符串比较
- 黑马学习笔记——MongoDB 标准连接字符串
- .Net连接字符串(ConnectionString)学习笔记