数据库连接
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
/// <summary>
///yishilalaDBSQL 的摘要说明
/// </summary>
public class yishilalaDBSQL
{
public yishilalaDBSQL()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
public bool TestConnect()
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data source=.\\SQLExpree;Integrated Security=true; AttachdbFilename=|DataDirectory|\\yishilala.mdf;User Instance=true";
try
{
conn.Open();
return (conn.State == ConnectionState.Open);
}
catch (Exception ex)
{
return false;
}
finally
{
conn.Close();
}
}
}
后台代码是:
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class web_New : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
yishilalaDBSQL db = new yishilalaDBSQL();
bool Connect = db.TestConnect();
Response.Write(Connect);
}
}
如果连接数据库成功,网页会显示true,而我的是显示false,表明是连接不成功,而测试时代码没有提示错误;本人是菜鸟;希望各位大虾多多指点!!
------解决方案--------------------
连接字符串中的实例名错了 是sqlexpress
------解决方案--------------------
你的数据库还没有附加上吗?
这个错误肯定是连接字符串有问题了