asp.net 连接数据库文件 的连接字符串该怎么写,大致我知道,只是文件路径那怎么用相对路径来写
2个回答
展开全部
web.config中配置
<add key="Conn" value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="/>
<add key="xbPhoto" value="~/App_Data/a.mdb"/>
创建一个类
public string strConn = ConfigurationSettings.AppSettings["Conn"].ToString();
public string dbXbPhoto = ConfigurationSettings.AppSettings["xbPhoto"].ToString();
public string GetDB() {
string NewConn = "";
NewConn = System.Web.HttpContext.Current.Server.MapPath(dbXbPhoto);
return NewConn;
}
<add key="Conn" value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="/>
<add key="xbPhoto" value="~/App_Data/a.mdb"/>
创建一个类
public string strConn = ConfigurationSettings.AppSettings["Conn"].ToString();
public string dbXbPhoto = ConfigurationSettings.AppSettings["xbPhoto"].ToString();
public string GetDB() {
string NewConn = "";
NewConn = System.Web.HttpContext.Current.Server.MapPath(dbXbPhoto);
return NewConn;
}
更多追问追答
追问
可以提供下 sql 数据库文件的吗?
追答
SQL:
CONN类:
using System;
using System.Web;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
///
/// App_Conn 的摘要说明
///
public class App_Conn
{
///
/// 得到数据库的地址
///
public static string ConnStr
{
get {
return ConfigurationManager.ConnectionStrings["ConStr"].ToString().Trim();
}
}
public App_Conn()
{}
public static DataSet _DataSet(string MySQL)
{
SqlConnection m_con = new SqlConnection(ConnStr);
if (m_con.State != ConnectionState.Open)
{
m_con.Open();
}
SqlCommand cmd = new SqlCommand(MySQL, m_con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
if (m_con.State == ConnectionState.Open)
{
m_con.Close();
m_con.Dispose();
}
return ds;
}
///
/// 执行SQL语句后,返回执行的行数
///
///
///
public static int _RowCount(string MySQL)
{
SqlConnection m_con = new SqlConnection(ConnStr);
if (m_con.State != ConnectionState.Open)
{
m_con.Open();
}
SqlCommand cmd = new SqlCommand(MySQL, m_con);
int rowscount = cmd.ExecuteNonQuery();
if (m_con.State == ConnectionState.Open) {
m_con.Close();
m_con.Dispose();
}
return rowscount;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询