在C#中连接数据库时相对路径该怎么用?
classDbAccess{privateOleDbConnectionconn;privateOleDbCommandcmd;///<summary>///实例化con...
class DbAccess
{
private OleDbConnection conn;
private OleDbCommand cmd;
/// <summary>
/// 实例化conn和设置cmd的链接
/// </summary>
public DbAccess() { //构造函数来初始化conn,cmd
conn = new OleDbConnection();
conn.ConnectionString = @"provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\我的设计\VS\会议记录系统\会议记录系统\App_Data\huiyineirong.mdb";//这个是绝对的路径
cmd = new OleDbCommand();
cmd.Connection = conn;
}
/// <summary>
/// 打开数据连接
/// </summary>
/// <returns></returns>
private bool OpenConn() {
if (conn.State != ConnectionState.Open)
{
conn.Open();
return true;
}
else
{ return false;
}
}
private bool CloseConn()
{
if (conn.State != ConnectionState.Closed)
{
conn.Close();
return true;
}
else
{
return false;
}
}
/// <summary>
/// 数据库中验证是否存在这个用户
/// </summary>
/// <param name="uid">用户uID</param>
/// <param name="upwd">密码</param>
/// <returns>找到返回true</returns>
internal bool DbCheckUser(string uid, string upwd)
{
cmd.CommandText = "select count(*) from yonghu where uid='"+ uid+"' and upwd='"+upwd+"'";
this.OpenConn();
int result = (int)cmd.ExecuteScalar();
this.CloseConn();
if (result > 0)
return true;
else
return false;
} 展开
{
private OleDbConnection conn;
private OleDbCommand cmd;
/// <summary>
/// 实例化conn和设置cmd的链接
/// </summary>
public DbAccess() { //构造函数来初始化conn,cmd
conn = new OleDbConnection();
conn.ConnectionString = @"provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\我的设计\VS\会议记录系统\会议记录系统\App_Data\huiyineirong.mdb";//这个是绝对的路径
cmd = new OleDbCommand();
cmd.Connection = conn;
}
/// <summary>
/// 打开数据连接
/// </summary>
/// <returns></returns>
private bool OpenConn() {
if (conn.State != ConnectionState.Open)
{
conn.Open();
return true;
}
else
{ return false;
}
}
private bool CloseConn()
{
if (conn.State != ConnectionState.Closed)
{
conn.Close();
return true;
}
else
{
return false;
}
}
/// <summary>
/// 数据库中验证是否存在这个用户
/// </summary>
/// <param name="uid">用户uID</param>
/// <param name="upwd">密码</param>
/// <returns>找到返回true</returns>
internal bool DbCheckUser(string uid, string upwd)
{
cmd.CommandText = "select count(*) from yonghu where uid='"+ uid+"' and upwd='"+upwd+"'";
this.OpenConn();
int result = (int)cmd.ExecuteScalar();
this.CloseConn();
if (result > 0)
return true;
else
return false;
} 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询