C#.net对Access数据库做的添加、删除、查找、修改的例子

 using System;
using System.Data;
using System.Data.OleDb;
using System.Collections;
using System.Windows.Forms;

namespace Race
{
/// <summary>
/// 数据库操作
/// </summary>
public class MyData
{
private OleDbConnection conn;
private OleDbCommand comm;

public MyData()//用函数实现一个数据库联接
{
conn = new OleDbConnection(@"Provider='Microsoft.Jet.OLEDB.4.0';Data Source='" + Application.StartupPath + @"/data.mdb'");
}

/// <summary>
/// 获取二级项目信息
/// </summary>
/// <param name="peid">一级项目ID</param>
/// <returns>二级项目信息</returns>
public DataTable Project_GetInfo()//从数据库中读取数据到da 再在内存中建立ds 用fill 来把da的数据填充到ds再返回ds 中的首行
{
try
{
OleDbDataAdapter da = new OleDbDataAdapter("select * from Project",conn);
DataSet ds = new DataSet();
da.Fill(ds);

return ds.Tables[0];

}
catch
{
return null;

}
}

/// <summary>
/// 添加数据库记录
/// </summary>
/// <param name="info">信息数组</param>
public void Project_InsertInfo(string[] info)
{
try
{
comm = new OleDbCommand("insert into [project] ([name],[multiple],[time],[delay],[loop],[music],[startmusic]) values ('"+info[0]+"','"+info[1]+"','"+info[2]+"','"+info[3]+"','"+info[4]+"','"+info[5]+"','"+info[6]+"')",conn);
conn.Open();
comm.ExecuteNonQuery();
}
catch
{
//MessageBox.Show("添加失败");
}
finally
{
conn.Close();
}
}

/// <summary>
/// 修改数据库记录
/// </summary>
/// <param name="info">信息数组</param>
/// <param name="id">ID</param>
public void Project_UpdateInfo(string[] info,string id)
{
try
{
comm = new OleDbCommand("update [project] set [name]='"+info[0]+"',[multiple]='"+info[1]+"',[time]='"+info[2]+"',[delay]='"+info[3]+"',[loop]='"+info[4]+"',[music]='"+info[5]+"',[startmusic]='"+info[6]+"' where id="+id,conn);
conn.Open();
comm.ExecuteNonQuery();
}
catch{}
finally
{
conn.Close();
}
}

/// <summary>
/// 删除数据库记录
/// </summary>
/// <param name="info">信息数组</param>123
public void Project_DeleteInfoByID(string id)
{
try
{
comm = new OleDbCommand("delete from project where [id]="+id,conn);
conn.Open();
comm.ExecuteNonQuery();

}
catch{}
finally
{
conn.Close();
}
}

}
}
展开阅读全文

没有更多推荐了,返回首页