没用过SQL Server 数据库,第一次配置费了不少劲,微软的东西有点绕,能遇到的问题都遇到了,看过此文后,数据库基本都能连接。废话不说。我的系统是xp,不能装SQL Server 2005的企业版本,只能装开发版和Express版,Express是免费版,学习用它就够了。首先需要下载一个名为SQLServer2005_SSMSEE.msi,这个文件一搜索就有下载(http://download.microsoft.com/download/1/1/0/110d908f-c445-4523-b939-220c7d135f3d/SQLServer2005_SSMSEE.msi)下载安装。用VS2005菜单 工具 连接到数据库 出现连接服务器资源管理器,右键 数据连接 如下图:
![](https://i-blog.csdnimg.cn/blog_migrate/a792736ca8f960c006fd19897656da9d.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/db80ee8c753d3d0881d73db2688bdf55.gif)
开始 -- Microsoft SQL Server 2005 -- SQL Server Management Studio Express,选择Windows 身份验证 连接 ,点上面的新建立查询 在右面输入 sp_password null,'123456','sa' ,点上面的执行,这样就设置了一个用户名为sa ,密码为:123456的用户,但是现在还不能用用户名sa登陆 如下图:
![](https://i-blog.csdnimg.cn/blog_migrate/2f222e0cf9bf261011f68d8bce1703e3.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/3b6585603d1f08d64858662cb7c073c4.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a25c41ad508823d89b27b455a2b13bc.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a792736ca8f960c006fd19897656da9d.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b1d50b26a3be584d5e745c8932668225.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/78c5d954ae5da6e16c672b4ea50227a2.gif)
1.web.config (配置文件) 中
<appSettings>
<add key="ConnectionString" value="serverlocalhost/sqlexpress;uid=sa;pwd=123456;database=News"/>
</appSettings>
调用的时候
string strConn = System.Configuration.ConfigurationManager.AppSettings["ConnectionString"].ToString();
SqlConnection Conn = new SqlConnection(strConn);
2.或者不用web.config直接在文件中写
SqlConnection conn = new SqlConnection("server=.//SQLEXPRESS;uid=sa;pwd=123456;database=login");
如何是Express版的数据库,一定要在服务器名的后面加上 //SSQLEXPRESS
一个完整的例子
string userName = Request.Form["userName"];
string userPwd = Request.Form["userPwd"];
SqlConnection con = new SqlConnection("server=localhost//SqlExpress;uid=sa;pwd=123456;database=login");
con.Open();
SqlCommand cmd=new SqlCommand("select count(*) from login where userName='"+userName+"' and userPwd='"+userPwd+"'",con);
int count=Convert.ToInt32(cmd.ExecuteScalar());
if(count>0)
{
Response.Redirect("main.aspx");
}