C#,asp.net 链接access 向access中插入数据。求具体步骤。
展开全部
/// <summary>
/// 通过SQl连接数据库
/// </summary>
public class SQL
{
public IDbConnection StarConnectSQL()
{
SqlConnection con = new SqlConnection();
con.ConnectionString = @"server=.;database=StudentDB;uid=sa;pwd=sasa";
con.Open();
return con;
}
/// <summary>
/// 数据库的操作,传参为数据库的执行语句string类型 返回为整型
/// </summary>
/// <param Name1="sql为数据库的执行语句"></param>
/// <returns></returns>
public int ExSql(string sql)
{
SqlCommand com = new SqlCommand();
com.CommandText = sql;
com.Connection = (SqlConnection)StarConnectSQL();
return com.ExecuteNonQuery();
}
/// <summary>
/// 返回的是表格
/// </summary>
/// <param Name1="sql"></param>
/// <returns></returns>
public DataSet ExSqlReDataSet(string sql)
{
SqlDataAdapter ad = new SqlDataAdapter(sql, (SqlConnection)StarConnectSQL());
DataSet ds = new DataSet();
ad.Fill(ds);
return ds;
}
/// <summary>
/// 返回为行值
/// </summary>
/// <param Name1="sql"></param>
/// <returns></returns>
public IDataReader ExSqlReReader(string sql)
{
SqlCommand cmd = new SqlCommand(sql, (SqlConnection)StarConnectSQL());
return cmd.ExecuteReader();
}
/// <summary>
/// 对数据库进行查询,查看内容是否为空。
/// </summary>
/// <param Name1="sql"></param>
/// <returns></returns>
public object SqlOrNULL(string sql)
{
SqlCommand cmd = new SqlCommand(sql, (SqlConnection)StarConnectSQL());
return cmd.ExecuteScalar();
}
}
/// <summary>
/// 通过Access连接数据库
/// </summary>
public class AccessSql
{
public IDbConnection StarConnectSQL()
{
OleDbConnection ODCAccess;
ODCAccess = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='数据库\ADO.accdb'");
ODCAccess.Open();
return ODCAccess;
}
public int ExSql(string sql)
{
OleDbCommand com = new OleDbCommand();
com.CommandText = sql;
com.Connection = (OleDbConnection)StarConnectSQL();
return com.ExecuteNonQuery();
}
public DataSet ExSqlReDataSet(string sql)
{
OleDbDataAdapter ad = new OleDbDataAdapter(sql,(OleDbConnection)StarConnectSQL());
DataSet ds = new DataSet();
ad.Fill(ds);
return ds;
}
public IDataReader ExSqlReReader(string sql)
{
OleDbCommand cmd = new OleDbCommand();
cmd.Connection =(OleDbConnection)StarConnectSQL();
cmd.CommandText = sql;
return cmd.ExecuteReader();
}
public object SqlOrNULL(string sql)
{
OleDbCommand cmd = new OleDbCommand(sql,(OleDbConnection)StarConnectSQL());
return cmd.ExecuteScalar();
}
}
函数所带参数string sql为数据库语句,包括增、删、改、查的语句。只要你的SQL语句正确就可以了。呵呵
/// 通过SQl连接数据库
/// </summary>
public class SQL
{
public IDbConnection StarConnectSQL()
{
SqlConnection con = new SqlConnection();
con.ConnectionString = @"server=.;database=StudentDB;uid=sa;pwd=sasa";
con.Open();
return con;
}
/// <summary>
/// 数据库的操作,传参为数据库的执行语句string类型 返回为整型
/// </summary>
/// <param Name1="sql为数据库的执行语句"></param>
/// <returns></returns>
public int ExSql(string sql)
{
SqlCommand com = new SqlCommand();
com.CommandText = sql;
com.Connection = (SqlConnection)StarConnectSQL();
return com.ExecuteNonQuery();
}
/// <summary>
/// 返回的是表格
/// </summary>
/// <param Name1="sql"></param>
/// <returns></returns>
public DataSet ExSqlReDataSet(string sql)
{
SqlDataAdapter ad = new SqlDataAdapter(sql, (SqlConnection)StarConnectSQL());
DataSet ds = new DataSet();
ad.Fill(ds);
return ds;
}
/// <summary>
/// 返回为行值
/// </summary>
/// <param Name1="sql"></param>
/// <returns></returns>
public IDataReader ExSqlReReader(string sql)
{
SqlCommand cmd = new SqlCommand(sql, (SqlConnection)StarConnectSQL());
return cmd.ExecuteReader();
}
/// <summary>
/// 对数据库进行查询,查看内容是否为空。
/// </summary>
/// <param Name1="sql"></param>
/// <returns></returns>
public object SqlOrNULL(string sql)
{
SqlCommand cmd = new SqlCommand(sql, (SqlConnection)StarConnectSQL());
return cmd.ExecuteScalar();
}
}
/// <summary>
/// 通过Access连接数据库
/// </summary>
public class AccessSql
{
public IDbConnection StarConnectSQL()
{
OleDbConnection ODCAccess;
ODCAccess = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='数据库\ADO.accdb'");
ODCAccess.Open();
return ODCAccess;
}
public int ExSql(string sql)
{
OleDbCommand com = new OleDbCommand();
com.CommandText = sql;
com.Connection = (OleDbConnection)StarConnectSQL();
return com.ExecuteNonQuery();
}
public DataSet ExSqlReDataSet(string sql)
{
OleDbDataAdapter ad = new OleDbDataAdapter(sql,(OleDbConnection)StarConnectSQL());
DataSet ds = new DataSet();
ad.Fill(ds);
return ds;
}
public IDataReader ExSqlReReader(string sql)
{
OleDbCommand cmd = new OleDbCommand();
cmd.Connection =(OleDbConnection)StarConnectSQL();
cmd.CommandText = sql;
return cmd.ExecuteReader();
}
public object SqlOrNULL(string sql)
{
OleDbCommand cmd = new OleDbCommand(sql,(OleDbConnection)StarConnectSQL());
return cmd.ExecuteScalar();
}
}
函数所带参数string sql为数据库语句,包括增、删、改、查的语句。只要你的SQL语句正确就可以了。呵呵
追问
感谢您的回答, 但是这是对sql server的吧? 我想链接的是access2007
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |