您的位置:首页 > 数据库

学习第十七天@数据库相关

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 问题: 连接字符串的处理 有时候我们需要获得连接字符串里的各个参数,比如说服务器,数据库,用户名,密码等等信息 刚开始的时候我没有发现现成的办法去取得这些信息,就是使用的对连接字符串直接分析 方法如下: ?
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); }

这个方法有个明显的缺陷就是连接字符串的格式被限制死了,如果更换顺序或者中间增加参数就无效了 今天在学习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;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: