求ASP.NET 操作 ACCESS 的例子。有增加、删除、修改、查找等基本功能!

c#的。可发百度HI。谢谢啦
2024-12-26 14:24:26
推荐回答(2个)
回答1:

using System;
using System.Collections.Generic;
using System.Text;
using System.Data.OleDb;
using System.Data;
using System.IO;

namespace Core.DBUtility
{
public class AccessClient
{
static string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["Conn"].ConnectionString;
public AccessClient()
{

}
public AccessClient(string path)
{
connectionString = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + path + @"\db.mdb";
}
///


/// 执行SQL语句,返回受影响的行数
///

/// SQL语句
/// 受影响的行数
public static int ExecuteNonQuery(string cmdText)
{
OleDbCommand cmd = new OleDbCommand();

using (OleDbConnection conn = new OleDbConnection(connectionString))
{
PrepareCommand(cmd, conn, CommandType.Text, cmdText, null);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val > 0 ? val : 0;
}
}
///
/// 执行SQL语句,返回OleDbDataReader
///

/// SQL语句
/// OleDbDataReader对象
public static OleDbDataReader ExecuteReader(string cmdText)
{
OleDbCommand cmd = new OleDbCommand();
OleDbConnection conn = new OleDbConnection(connectionString);
try
{
PrepareCommand(cmd, conn, CommandType.Text, cmdText, null);
OleDbDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return rdr;
}
catch
{
conn.Close();
throw;
}
}
///
/// 执行SQL语句,返回结果集第一行第一列
///

/// SQL语句
/// 结果集第一行第一列
public static object ExecuteScalar(string cmdText)
{
OleDbCommand cmd = new OleDbCommand();

using (OleDbConnection conn = new OleDbConnection(connectionString))
{
PrepareCommand(cmd, conn, CommandType.Text, cmdText, null);
object val = cmd.ExecuteScalar();
cmd.Parameters.Clear();
return val;
}
}
///
/// 执行SQL语句,返回DataTable
///

/// SQL语句
/// DataTable
public static DataTable ExecuteDataTable(string cmdText)
{
OleDbCommand comm = new OleDbCommand();
using (OleDbConnection conn = new OleDbConnection(connectionString))
{
PrepareCommand(comm, conn, CommandType.Text, cmdText, null);
OleDbDataAdapter adapter = new OleDbDataAdapter(comm);
DataSet ds = new DataSet();
adapter.Fill(ds, "temp");
return ds.Tables["temp"];
}
}

#region ExecuteSQL Method
public static int ExecuteNonQuery(CommandType cmdType, string cmdText, params OleDbParameter[] commandParameters)
{
OleDbCommand cmd = new OleDbCommand();

using (OleDbConnection conn = new OleDbConnection(connectionString))
{
PrepareCommand(cmd, conn, cmdType, cmdText, commandParameters);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
}

public static OleDbDataReader ExecuteReader(CommandType cmdType, string cmdText, params OleDbParameter[] commandParameters)
{
OleDbCommand cmd = new OleDbCommand();
OleDbConnection conn = new OleDbConnection(connectionString);
try
{
PrepareCommand(cmd, conn, cmdType, cmdText, commandParameters);
OleDbDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return rdr;
}
catch
{
conn.Close();
throw;
}
}

public static object ExecuteScalar(CommandType cmdType, string cmdText, params OleDbParameter[] commandParameters)
{
OleDbCommand cmd = new OleDbCommand();

using (OleDbConnection connection = new OleDbConnection(connectionString))
{
PrepareCommand(cmd, connection, cmdType, cmdText, commandParameters);
object val = cmd.ExecuteScalar();
cmd.Parameters.Clear();
return val;
}
}

public static DataTable ExecuteDataTable(CommandType cmdType, string cmdText, params OleDbParameter[] commandParameters)
{
OleDbCommand comm = new OleDbCommand();

using (OleDbConnection conn = new OleDbConnection(connectionString))
{
PrepareCommand(comm, conn, cmdType, cmdText, commandParameters);
OleDbDataAdapter adapter = new OleDbDataAdapter(comm);
DataSet ds = new DataSet();
adapter.Fill(ds, "temp");
return ds.Tables["temp"];
}
}

private static void PrepareCommand(OleDbCommand cmd, OleDbConnection conn, CommandType cmdType, string cmdText,OleDbParameter[] cmdParms)
{
if (conn.State != ConnectionState.Open)
conn.Open();
cmd.Connection = conn;
cmd.CommandText = cmdText;
cmd.CommandType = cmdType;

if (cmdParms != null)
{
foreach (OleDbParameter parm in cmdParms)
cmd.Parameters.Add(parm);
}
}
#endregion
}
}

回答2:

楼上的应该能实现,不过你在绑定数据源某个表后选择“高级”,将里面的两项选中,即可实现次控件自带的增,删,改,分页,选定等常用功能