我在网上当的一个网站项目,开始放在D盘。里面的数据库也在D盘里。数据库附加路径为D:\vs2008project\XXGLBBS.mdf
现在我把这个项目放到了f盘。把数据库名字改成了SWBBS.mdf ..重新附加数据库。为什么连不上了, “无法打开登录所请求的数据库 "XXGLBBS"。登录失败。“ 我数据库名字已经该过了,项目路径改到f 盘了,为什么还连接不上
现在我把这个项目放到了f盘。把数据库名字改成了SWBBS.mdf ..重新附加数据库。为什么连不上了, “无法打开登录所请求的数据库 "XXGLBBS"。登录失败。“ 我数据库名字已经该过了,项目路径改到f 盘了,为什么还连接不上
解决方案 »
- Oracle缺失表达式
- 提问:关于WebRequest.ContentLength的属性
- .net如何转换成16进制数?
- 关于用三层结构开发web应用程序
- 求高人用简洁的话来描述OutputCache的作用
- 菜鸟求教!
- 问一个数据库 绑定的 简单问题~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~马上给分
- ★★急救!用Input 控件来打开一个本地上的图片后,怎样将它及时的显示在Image上?
- 后台取页面上值的问题,大家快来帮忙呀!都来学习呀!
- 用asp.net做成outlook中附件那樣的(幫忙....)
- 在ASP.NET中如何引用Windows Media Player插件
- 关于DataList的CheckBox的更新问题
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];
}
}
}
把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>
string connstr = "Data Source=SWei\\SQLEXPRESS;Initial Catalog=SWBBS;User id=sa;pwd=880827";