做一个动态网站.怎样使它与数据库相连

2024-12-23 03:13:35
推荐回答(2个)
回答1:

public class conn
{
private OleDbConnection myconn = new OleDbConnection();

public conn()
{
string str = @"provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + System.Web.HttpContext.Current.Server.MapPath(@"~\App_Data\data.mdb");
myconn.ConnectionString = str;
myconn.Open();
}
///


/// 察看符合检索出的记录集条数
///

/// 需要检查select语句
/// 记录条数
public static int TestRs(string sql)
{
conn conn = new conn();
OleDbCommand cmd = conn.myconn.CreateCommand();
cmd.CommandText = sql;
OleDbDataReader myrs = cmd.ExecuteReader();
int count = 0;
while (myrs.Read())
{
count++;
}
myrs.Close();
conn.myconn.Close();
return count;
}
///
/// 返回有记录集的查询方法的子类
///

public class cmd1
{
public OleDbDataReader myrs;
conn conn;
public cmd1(string sql)
{
try
{
conn = new conn();
OleDbCommand cmd = conn.myconn.CreateCommand();
cmd.CommandText = sql;
myrs = cmd.ExecuteReader();
}
catch
{
myrs = null;
}
}
public void clod()
{
myrs.Close();
conn.myconn.Close();
}
}

///
/// 无返回纪录集的方法
///

/// 查询字符串
public static void cmd2(string st)
{
conn conn = new conn();
OleDbCommand cmd = conn.myconn.CreateCommand();
cmd.CommandText = st;
cmd.ExecuteNonQuery();
conn.myconn.Close();
}

public static DataTable get_dt(string a1)
{
try
{
conn conn = new conn();
OleDbCommand cmd = conn.myconn.CreateCommand();
cmd.CommandText = a1;
DataTable dt = new DataTable();
OleDbDataAdapter olda = new OleDbDataAdapter();
DataSet ds = new DataSet();
olda.SelectCommand = cmd;
olda.Fill(ds, "[user]");
dt = ds.Tables[0];
return dt;
}
catch
{
return null;
}
}
///
/// md5 加密
///

/// 字符
/// 字符
public static string md5(string tt)
{
string st1= System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(tt,"MD5");
string st2 = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(st1,"MD5");
string st3 = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(st2, "MD5");
return st1 + st2 + st3;
}
}
ASP。NET中的连接数据库(ACCESS)的

回答2:

asp文件
使用access数据库/`