注册 登录
编程论坛 ASP技术论坛

新手请教vs2005连接sql2000字符串?

chenhao19860 发布于 2010-06-27 22:25, 878 次点击
请各位师兄师姐帮忙,给了就借。
3 回复
#2
yms1232010-06-28 13:44
应该可以用VS2005自动生成连接字符串
#3
yms1232010-06-28 13:50
VS2005里服务器资源管理器,建立数据库连接,根据提示来选择SQL2000,连接成功后在服务器资源管理器点击数据库节点,属性窗口ConnectionString里就是系统自动生成的连接字符串。
#4
jingjing3162010-06-30 16:02
VS2005中连接SQL Server2000 连接字符串2009-11-05 22:54private static string connString = "Server=(local);database=WebDevelop;uid=sa;pwd=sa";

这里的(local)还可以是localhost或者127.0.0.1

从这里可以看出,应该写服务器的地址,由于是本地运行,而且在IIS中绑定了127.0.0.1这个地址,所以写localhost和127.0.0.1都可以。另外,(local)是SQL Server 2000 中的服务器对象,所以也是可以的。如果在IIS中将此网站设为默认网站,而且绑定地址169.254.218.201,则还可以这样连接:

private static string connString = "Server=169.254.218.201;database=WebDevelop;uid=sa;pwd=sa";

如果是数据库在远程服务器上运行,则此处应该写远程服务器的IP地址。

另外,打开和读取数据库 中数据时,很容易抛出异常,所以不要经常捕捉,否则会出错

//连接数据库
        SqlConnection conn = new SqlConnection(SqlConn.ConnString);
        string sqlText = "SELECT * FROM LoginUser WHERE (username='" + name + "') AND (" + "userpwd='" + pwd + "')";
        SqlCommand command = new SqlCommand(sqlText, conn);
        //try
        //{
            conn.Open();
            //try to get the information and identification
            //try
            //{
                SqlDataReader dataReader = command.ExecuteReader();//SqlDataReader doesn't have a constructor
                if (dataReader.Read() == true)
                {
                    Response.Redirect("ModelList.aspx");
                }
                else
                {
                    Response.Redirect("LogErr.aspx");
                }
            //}
            //catch (System.Exception exception)
            //{
            //    Response.Redirect("SysErr.aspx");
            //}
            conn.Close();

        //}
        //catch (System.Exception excetion1)
        //{
        //    Response.Redirect("OpenDatabaseErr.aspx");
        //}

 
1