我在网上当的一个网站项目,开始放在D盘。里面的数据库也在D盘里。数据库附加路径为D:\vs2008project\XXGLBBS.mdf
现在我把这个项目放到了f盘。把数据库名字改成了SWBBS.mdf ..重新附加数据库。为什么连不上了, “无法打开登录所请求的数据库 "XXGLBBS"。登录失败。“   我数据库名字已经该过了,项目路径改到f 盘了,为什么还连接不上

解决方案 »

  1.   

    原来的是一个.cs文件using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Data;
    using System.Data.SqlClient;
    namespace BBS.DAL
    {
        public class DBHelper
        {
            private static SqlConnection getConnection;        public static SqlConnection GetConnection
            {
                get
                {
                    string connstr = "Data Source=SWei\\SQLEXPRESS;Initial Catalog=XXGLBBS;User id=sa;pwd=880827";
                     SqlConnection getConnection= new SqlConnection(connstr);
                       //getConnection.Open();
                 if (getConnection == null)
               {
                      getConnection = new SqlConnection(connstr);
                        getConnection.Open();
                  }
                   else if (getConnection.State == ConnectionState.Closed)
                    {
                        getConnection = new SqlConnection(connstr);
                        getConnection.Open();
                    }
                    else if (getConnection.State == ConnectionState.Broken)
                    {
                        getConnection = new SqlConnection(connstr);
                        getConnection.Open();
                    }
                    return getConnection;
               }        }        //执行Sql语句
            public static int ExecuteCommand(string sql)
            {
                SqlCommand cmd = new SqlCommand(sql, GetConnection);
                int result = Convert.ToInt32(cmd.ExecuteNonQuery());
                return result;
            }        //执行带参数的Sql语句
            public static int ExecuteCommand(string sql, SqlParameter[] sp)
            {
                SqlCommand cmd = new SqlCommand(sql, GetConnection);
                cmd.Parameters.AddRange(sp);
                int result = Convert.ToInt32(cmd.ExecuteNonQuery());
                return result;
            }        //执行Sql语句
            public static int GetScalar(string sql)
            {
                SqlCommand cmd = new SqlCommand(sql, GetConnection);
                int result = Convert.ToInt32(cmd.ExecuteScalar());
                return result;
            }        //执行带参数的Sql语句
            public static int GetScalar(string sql, SqlParameter[] sp)
            {
                SqlCommand cmd = new SqlCommand(sql, GetConnection);
                cmd.Parameters.AddRange(sp);
                int result = Convert.ToInt32(cmd.ExecuteScalar());
                return result;
            }        //查询返回记录集对象
            public static SqlDataReader GetReader(string sql)
            {
                SqlCommand cmd = new SqlCommand(sql, GetConnection);
                SqlDataReader reader = cmd.ExecuteReader();
                return reader;
            }        //查询带参返回记录集对象
            public static SqlDataReader GetReader(string sql, SqlParameter[] sp)
            {
                SqlCommand cmd = new SqlCommand(sql, GetConnection);
                cmd.Parameters.AddRange(sp);
                SqlDataReader reader = cmd.ExecuteReader();
                return reader;
            }        //返回表格的查询
            public static DataTable GetDataSet(string sql)
            {
                DataSet ds = new DataSet();
                SqlCommand cmd = new SqlCommand(sql, GetConnection);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(ds);
                return ds.Tables[0];
            }        //带参数的查询返回表
            public static DataTable GetDataSet(string sql, SqlParameter[] sp)
            {
                DataSet ds = new DataSet();
                SqlCommand cmd = new SqlCommand(sql, GetConnection);
                cmd.Parameters.AddRange(sp);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(ds);
                return ds.Tables[0];
            }
        }
    }
      

  2.   

    数据库的名字是XXGLBBS.mdf ,那么数据库的一些信息是保存在XXGLBBS.mdf 中的,其中包括数据库的名字XXGLBBS,当你改为SWBBS之后,SWBBS.mdf中存在的名字还是XXGLBBS,所以会出错
      

  3.   

    这样的话应该要修改Web.config里面的数据库连接吧~~你找找Web.config里面的<connectionStrings>
    把Database属性修改成你的数据库名称。
    比如有个连接是这样:
    <connectionStrings>
    <add name="NorthWind" connectionString="Server=127.0.0.1;Database=
    NorthWind; Integrated Security=SSPI;Persist Security Info=True" 
    providerName="System.Data.SqlClient"/>
    </connectionStrings>
      

  4.   

    唉数据库管理里修改XXGLBBS为SWBBS,再分离数据库,移动数据库到其他路径
    string connstr = "Data Source=SWei\\SQLEXPRESS;Initial Catalog=SWBBS;User id=sa;pwd=880827"; 
      

  5.   

    你的文件名称改了,但数据库名称却是旧的,所以你的连接信息中,databasename还是要有xxxGLBBS