学习第十七天@数据库相关
2012-02-20 22:17
330 查看
问题: 使用SqlConnection对象连接SQL Server数据库 声明:以下连接的属性都可以参考“SQL Server 数据库连接字符串参数一览表”取它的别名;除了必须设置的属性以外还可以设置其他辅助的属性。如Connect Timeout、Encrypt等 设置数据库文件路径的方法: 1.使用绝对路径:“AttachDbFilename=D:\\Solution1\\Web\\App_Data\\data.mdf” 2.使用服务器相对路径:“AttachDbFilename=”+Server.MapPath(“\\App_Data\\data.mdf”) 3.使用最简单的相对路径:“AttachDbFilename=|DataDirectory|\\data.mdf” 推荐使用第3种方式,“|DataDirectory|”代表ASP.NET项目里自动创建的App_Data文件夹 1.以SQL Server验证模式连接SQLServer (1)以数据库名连接方式 1. Server=服务器名; 2. Database=数据库名称; 3. User ID=用户名; 4. Password=密码 或者(使用缩写与别名) 1. Server=服务器名; 2. Initial Catalog=数据库名称; 3. Uid=用户; 4. Pwd=密码 (2)以数据库文件完整路径连接方式 “Serve=服务器名;AttachDbFilename=数据库文件路径;User ID=用户名;Password=密码” 示例: 1. Server=.\SQLEXPRESS; Database=DatabaseName; User ID =sa; Password=abc123” 2. Server=.\SQLEXPRESS; Initial Catalog =DatabaseName; Uid =sa; Pwd=abc123” 3. Server=(local)\SQLEXPRESS; AttachDbFilename=D:\\Solution1\\Web\\App_Data\\data.mdf; 4. User ID =sa; Password=abc123” 备注:密码可以为空。 2.以Windows 验证模式连接SQL Server (1)以数据库名连接方式 1. Server=服务器名; 2. Database=数据库名称; 3. Integrated Security=SSPI (2)以数据库文件完整路径连接方式 “Serve=服务器名;AttachDbFilename=数据库文件路径; Integrated Security=true” 示例: 1. Server=服务器名; 2. Database=数据库名称; 3. Integrated Security=SSPI 4. Server=(local)\SQLEXPRESS; 5. AttachDbFilename=D:\\Solution1\\Web\\App_Data\\data.mdf; 6. Integrated Security=true” 备注:SSPI即为true 问题: 连接字符串的处理 有时候我们需要获得连接字符串里的各个参数,比如说服务器,数据库,用户名,密码等等信息 刚开始的时候我没有发现现成的办法去取得这些信息,就是使用的对连接字符串直接分析 方法如下: ?
这个方法有个明显的缺陷就是连接字符串的格式被限制死了,如果更换顺序或者中间增加参数就无效了 今天在学习C#3.0的时候发现了微软在.net2.0就增加了的实例成员,可以直接读取,设置字符串的各种参数 需要引用System.Data.SqlClient命名空间,它底下有个类SqlConnectionStringBuilder,通过这个类的实例我们可以创建或者管理连接字符串. string cc = ConfigurationManager.ConnectionStrings["conn"].ToString(); SqlConnectionStringBuilder sb=new SqlConnectionStringBuilder(cc); sb.DataSource;//服务器名称 sb.InitialCatalog;//数据库名 sb.UserID;//用户名 sb.Password;//密码 很明显微软自带的方法是很好使的,这个类还有其他成员,请自行查看 问题: 如何使用SQL查找子串首次出现的位置?
当不存在时,返回0:
问题: 没有开启xp_cmdshell? SQL Server 已封鎖元件 'xp_cmdshell' 的 程序 'sys.xp_cmdshell' 之存取,因為此元件已經由此伺服器的安全性組態關閉。系統管理員可以使用 sp_configure 來啟用 'xp_cmdshell' 的使用。如需有關啟用 'xp_cmdshell' 的詳細資訊,請參閱《SQL Server 線上叢書》中的<介面區組態>(Surface Area Configuration)。 用下面一句话就可以了解决了。 EXEC sp_configure 'show advanced options', 1;RECONFIGURE;EXEC sp_configure 'xp_cmdshell', 1;RECONFIGURE; 关闭一样.只是将上面的后面的那个"1"改成"0"就可以了. EXEC sp_configure 'show advanced options', 1;RECONFIGURE;EXEC sp_configure 'xp_cmdshell', 0;RECONFIGURE;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | ///conn是连接字符串的Name string cc = ConfigurationManager.ConnectionStrings["conn"].ToString(); int firstindex = 0; int lastIndex = 0; Sname = SubStr(cc, ref firstindex, ref lastIndex);//服务器名称 SubStr(cc, ref firstindex, ref lastIndex);//数据库名 Uname = SubStr(cc, ref firstindex, ref lastIndex);//用户名 Passwd = SubStr(cc, ref firstindex, ref lastIndex);//密码 /// <summary> /// 获取"="和";"之间的字符串 /// </summary> /// <span name="cc"></span>连接字符串 /// <span name="firstindex"></span>"="的位置 /// <span name="lastIndex"></span>";"的位置 private string SubStr(string cc, ref int firstindex, ref int lastIndex) { firstindex = cc.IndexOf("=", lastIndex); lastIndex = cc.IndexOf(";", firstindex); return cc.Substring(firstindex + 1, lastIndex - firstindex - 1); } |
当不存在时,返回0:
问题: 没有开启xp_cmdshell? SQL Server 已封鎖元件 'xp_cmdshell' 的 程序 'sys.xp_cmdshell' 之存取,因為此元件已經由此伺服器的安全性組態關閉。系統管理員可以使用 sp_configure 來啟用 'xp_cmdshell' 的使用。如需有關啟用 'xp_cmdshell' 的詳細資訊,請參閱《SQL Server 線上叢書》中的<介面區組態>(Surface Area Configuration)。 用下面一句话就可以了解决了。 EXEC sp_configure 'show advanced options', 1;RECONFIGURE;EXEC sp_configure 'xp_cmdshell', 1;RECONFIGURE; 关闭一样.只是将上面的后面的那个"1"改成"0"就可以了. EXEC sp_configure 'show advanced options', 1;RECONFIGURE;EXEC sp_configure 'xp_cmdshell', 0;RECONFIGURE;
相关文章推荐
- oracle数据库学习总结:数据库概念及相关信息查看
- MySQL服务器学习笔记!(一) ——数据库相关概念
- R语言的读取文件的相关学习(读取数据库,网页,EXCEL文件)
- 【ThinkPHP学习】数据库设计相关问题
- 关系型数据库相关学习
- QT学习5:数据库,sqlite相关
- 数据库监控相关学习
- 20100811 学习记录:数据库相关的自动编号& update
- MySQL服务器学习笔记!(一) ——数据库相关概念
- 数据库学习相关常用语句
- 工作流Activiti的学习总结(三) activit数据库限制和数据库相关的配置
- MySQL学习总结----数据库相关操作
- 推书(数据库及机器学习相关)
- 巴途_数据库存储引擎学习之《mysql,innoDB,MyISAM等相关数据库存储引擎分析》
- Liferay 6开发学习(二十六):数据库连接相关问题
- 黑马程序员之SQL学习笔记: 数据库相关知识 小总结
- SQLserver学习day01 SQLserver简介 数据库的相关概念 创建删除数据库操作
- 【关系模型相关概念】小楼一夜听春雨——数据库学习之路(3)
- 数据库相关学习
- 相关滤波目标跟踪学习笔记(二)——数据库简介