寻找c#对access操作的示例

貌似我没积分,不好意思了不过:谢谢你!想学习ado.net连access,C#对access的一些操作,想找一些例题代码什么的,学学。可以的话帮忙发些,3Q!... 貌似我没积分,不好意思了 不过:谢谢你!想学习ado.net连access,C#对access的一些操作,想找一些例题代码什么的,学学。可以的话帮忙发些,3Q! 展开
 我来答
匿名用户
2013-07-29
展开全部
这是我曾经练手的一个程序中的代码。增删改查基本都来了。这里提醒一句,使用access数据库的时候,一定要注意尽量不用关键字(有些关键字估计你压根都不知道是关键字,比如user,不然报的错会让你莫名其妙)。但是不是不能用,加个中括号就好。我的access数据库是加密了的,aap.config文件的代码如下:<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<connectionStrings>
<add name="DataBaseOwner" connectionString="dbo"/>
<add name="XinXingConnectionString" connectionString=
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db2.mdb;User ID=Admin;Jet OLEDB:Database Password=密码"
providerName ="Microsoft.Jet.OLEDB.4.0"/>
</connectionStrings>
<appSettings>
<add key="DBType" value="Sql"/>
</appSettings>
</configuration>具体增删改查的代码如下:using System;
using System.Collections.Generic;
using System.Text;
using System.Data.OleDb;
using System.Configuration;
using XinXingModels;namespace XinXingDAL
{
public class ProgramService
{
#region Private Members
//从配置文件中读取数据库连接字符串
private readonly string connString =
ConfigurationManager.ConnectionStrings["XinXingConnectionString"].ToString();
private readonly string dboOwner =
ConfigurationManager.ConnectionStrings["DataBaseOwner"].ToString();
#endregion /// <summary>
/// 查询所有账户信息
/// </summary>
/// <returns></returns>
public IList<Account> SearchAllRemarks()
{
List<Account> account = new List<Account>();
using (OleDbConnection conn = new OleDbConnection(connString))
{
OleDbCommand objCommand = new OleDbCommand("select * from Account", conn);
conn.Open();
OleDbDataReader objReader = objCommand.ExecuteReader();
while (objReader.Read())
{
Account a = new Account();
a.Id = Convert.ToInt32(objReader["ID"]);
a.Remarks = Convert.ToString(objReader["Remarks"]);
a.LoginID = Convert.ToString(objReader["LoginID"]);
a.Pwd = Convert.ToString(objReader["Pwd"]);
a.TwoPwd = Convert.ToString(objReader["TwoPwd"]);
a.Type = Convert.ToString(objReader["Type"]);
account.Add(a);
}
objReader.Dispose(); conn.Close();
conn.Dispose();
}
return account;
}
/// <summary>
/// 根据ID和备注查询相关信息
/// </summary>
/// <param name="id"></param>
/// <param name="remarks"></param>
/// <returns></returns>
public IList<Account> SelectInfoByRemarksAndID(int id, string remarks)
{
List<Account> account = new List<Account>();
using (OleDbConnection conn = new OleDbConnection(connString))
{
Account a = new Account();
string sql = string.Format("select LoginID,Pwd,TwoPwd,Type from Account Where Id={0} and remarks='{1}'", id, remarks);
OleDbCommand objCommand = new OleDbCommand(sql, conn);
conn.Open();
OleDbDataReader objReader = objCommand.ExecuteReader();
if (objReader.Read())
{
a.LoginID = Convert.ToString(objReader["LoginID"]);
a.Pwd = Convert.ToString(objReader["Pwd"]);
a.Type = Convert.ToString(objReader["Type"]);
a.TwoPwd = Convert.ToString(objReader["TwoPwd"]);
account.Add(a);
}
}
return account;
}
/// <summary>
/// 查询所有备忘录
/// </summary>
/// <returns></returns>
public IList<Notepad> SearchAllInfoFromNotepad()
{
List<Notepad> notepad = new List<Notepad>();
using (OleDbConnection conn = new OleDbConnection(connString))
{
OleDbCommand objCommand = new OleDbCommand("select * from Notepad", conn);
conn.Open();
OleDbDataReader objReader = objCommand.ExecuteReader();
while (objReader.Read())
{
Notepad n = new Notepad();
n.Id = Convert.ToInt32(objReader["ID"]);
n.Time = Convert.ToDateTime(objReader["Times"]);
n.Content = Convert.ToString(objReader["Content"]);
notepad.Add(n);
}
objReader.Dispose();
conn.Close();
conn.Dispose();
}
return notepad;
}
/// <summary>
/// 将备忘录插入
/// </summary>
/// <returns></returns>
public int InsertAllInfoIntoNotepad(string note)
{
int id;
using (OleDbConnection conn = new OleDbConnection(connString))
{
string sql = string.Format("insert into Notepad(content) values('{0}')", note);
OleDbCommand objCommand = new OleDbCommand(sql, conn);
conn.Open();
id = objCommand.ExecuteNonQuery();
}
return id;
}
/// <summary>
/// 查询最后一条记录
/// </summary>
/// <returns></returns>
public string SelectLastNotepad()
{
string message = "\n";
using (OleDbConnection conn = new OleDbConnection(connString))
{
string sql = string.Format("SELECT top 1 * from notepad order by ID desc");
OleDbCommand objCommand = new OleDbCommand(sql, conn);
conn.Open();
OleDbDataReader objReader = objCommand.ExecuteReader();
while (objReader.Read())
{
message += Convert.ToDateTime(objReader["Times"]) + "\n";
message += Convert.ToString(objReader["Content"]);
}
}
return message;
} public int InsertAccount(string loginID, string pwd,string twoPwd,string remarks,string type)
{
int id;
using (OleDbConnection conn = new OleDbConnection(connString))
{
string sql = string.Format("insert into Account(LoginID,Pwd,TwoPwd,Remarks,Type) values('{0}','{1}','{2}','{3}','{4}')", loginID, pwd, twoPwd, remarks, type);
OleDbCommand objCommand = new OleDbCommand(sql, conn);
conn.Open();
id = objCommand.ExecuteNonQuery();
}
return id;
}
}}
匿名用户
2013-07-29
展开全部
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Data;namespace DBUtility
{
public class olehelper
{
private OleDbConnection conn;
static string connProvider = System.Configuration.ConfigurationSettings.AppSettings["ConnProvider"]; static string connSource = System.Web.HttpContext.Current.Server.MapPath(System.Configuration.ConfigurationSettings.AppSettings["ConnSource"]);
public olehelper()//此为构造函数
{
conn = new OleDbConnection(connProvider + connSource);
}
/// <summary>
/// 返回一个可用的连接
/// </summary>
/// <returns></returns>
public OleDbConnection getcon()
{
conn.ConnectionString = connProvider;
try
{
conn.Open();
}
catch (Exception ex)
{ throw ex;
}
return conn;
}
/// <summary>
/// 关闭数据库连接
/// </summary>
public void CloseDB()
{
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
} /// <summary>
/// 执行增加、删除、修改操作
/// </summary>
/// <param name="sql">SQL语句</param>
/// <param name="parm">sql语句参数数组</param>
/// <returns></returns>
public int ExcueteCommand(string sql, params OleDbParameter[] parm)
{
//SqlCommand cmd = getcon().CreateCommand();
OleDbCommand omd = getcon().CreateCommand();
//cmd.CommandType = CommandType.Text;
omd.CommandType = CommandType.Text;
omd.CommandText = sql;
int result = -1;
if (parm != null)
{
//cmd.Parameters.AddRange(parm);
omd.Parameters.AddRange(parm);
}
try
{
//result = cmd.ExecuteNonQuery();
result = omd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw ex;
}
finally
{
//cmd.Dispose();
omd.Dispose();
CloseDB();
}
return result; } /// <summary>
/// 执行查询操作填充到dataset中
/// </summary>
/// <param name="sql">SQL语句</param>
/// <param name="parm">SQL语句参数数组</param>
/// <returns></returns>
public DataSet getds(string sql, params OleDbParameter[] parm)
{
//SqlDataAdapter sda = new SqlDataAdapter(sql, this.Connectionstring);
OleDbDataAdapter oda = new OleDbDataAdapter(sql, connProvider);
DataSet ds = new DataSet();
if (parm != null)
{
//sda.SelectCommand.Parameters.AddRange(parm);
oda.SelectCommand.Parameters.AddRange(parm);
}
try
{
//sda.Fill(ds);
oda.Fill(ds);
}
catch (Exception ex)
{ throw ex;
}
finally
{
//sda.Dispose();
oda.Dispose();
}
return ds;
} public OleDbDataReader GetReader(string sql, params OleDbParameter[] parm)
{ OleDbCommand omd = new OleDbCommand(sql, getcon()); omd.Parameters.AddRange(parm);
//SqlDataReader reader = cmd.ExecuteReader();
OleDbDataReader reader = omd.ExecuteReader();
return reader; } public OleDbDataReader GetReader(string safeSql)
{
OleDbCommand omd = new OleDbCommand(safeSql, getcon());
OleDbDataReader reader = omd.ExecuteReader();
return reader;
} //执行查询语句
public OleDbDataReader ExecuteReader(string sql)
{
OleDbDataReader reader = null; //查询结果
try
{ OleDbCommand somd = new OleDbCommand(sql, getcon());
reader = somd.ExecuteReader();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return reader;
conn.Close();
}
//执行带参查询语句
public OleDbDataReader ExecuteReader(string sql, params OleDbParameter[] sp)
{
OleDbDataReader reader = null; //查询结果
try
{
//Connect.Open();
OleDbCommand odc = new OleDbCommand(sql, getcon()); odc.Parameters.AddRange(sp);
reader = odc.ExecuteReader(); }
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
} return reader;
conn.Close();
} }
}
呵呵,我正在做,这是操作数据库的类
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2013-07-29
展开全部
给你一个操作ACCESS数据库的类,里面的方法都是静态的,用着很舒服啊。using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;namespace access
{
class DB_Access
{
private string path;
private OleDbConnection myCon;
private OleDbCommand myCom; public DB_Access(string strPath)
{
this.path = strPath;
String strCon = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=";
strCon += this.path;
myCon = new OleDbConnection(strCon);
myCon.Open();
myCom = new OleDbCommand();
myCom.Connection = myCon;
} public OleDbCommand GetCommand()
{
return myCom;
} public int Execute(string sql)
{
myCom.CommandText = sql;
return myCom.ExecuteNonQuery();
} public DataTable Query(string sql)
{
myCom.CommandText = sql;
OleDbDataAdapter adapter = new OleDbDataAdapter();
adapter.SelectCommand = myCom;
DataTable Dt = new DataTable();
adapter.Fill(Dt);
return Dt;
} public void Close()
{
myCon.Close();
} }
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2013-07-29
展开全部
这个很简单哈,LZ可以自己练习下哈,我把步骤给你说下:1.你先利用microsoft office access做一个数据库哈2.打开vs2008开发工具,数据库源控件,把该文件绑定到数据源(选择控件的时候要选择access数据源哈)3.添加一个gridview控件哈,把access数据文件的字段绑定到gridview控件,保存运行之,就OK哈。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2013-07-29
展开全部
晕~~直接发网址还不让审核通过。具体的例子看下面,解释的很清楚。我推荐你连接access的话最好用oledb来实现。http://www.cppblog.com/mzty/archive/2005/11/23/1269.html
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(3)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式