using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.OleDb;
namespace 链接数据库
{
class Access
{
OleDbConnection oleDb = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\王宗钦\Desktop\first.mdb");
public Access()
{
oleDb.Open();
}
public static void Show(DataRow a)
{
Console.WriteLine("{0} | {1} | {1} | {2} | {3} | {4} | {5}", a[0], a[1], a[2], a[3], a[4], a[5]);
}
public void Get()
{
string sql = "SELECT * FROM 表1";
OleDbDataAdapter dbDateAdapter = new OleDbDataAdapter(sql, oleDb);
DataTable dt = new DataTable();
dbDateAdapter.Fill(dt);
foreach(DataRow item in dt.Rows)
{
Show(item);
}
Console.WriteLine("--------------------------------------\n");
}
public void Find()
{
string sql = "SELECT * FROM 表1 " +
"WHERE 班级='17数媒02' "+
"AND 分数>80 ";
OleDbDataAdapter dbDataAdapter = new OleDbDataAdapter(sql, oleDb);
DataTable dt = new DataTable();
dbDataAdapter.Fill(dt);
foreach (DataRow item in dt.Rows)
Show(item);
Console.WriteLine("-----------------------------------------------------\n");
}
public bool Add()
{
string sql = "insert into 表1 (姓名,性别,班级,宿舍,分数)" +
"values ('gxz','男','17数媒02','114',50)";
OleDbCommand oleDbCommand = new OleDbCommand(sql, oleDb);
int i = oleDbCommand.ExecuteNonQuery();
string aa = "select * from 表1";
OleDbDataAdapter a = new OleDbDataAdapter(aa, oleDb);
DataTable dt = new DataTable();
a.Fill(dt);
foreach (DataRow item in dt.Rows)
Show(item);
Console.WriteLine("---------------------------------------------------------\n");
return i > 0;
}
public bool Del()
{
string sql = "delete from 表1 " +
"where 姓名='sck' ";
OleDbCommand olc = new OleDbCommand(sql, oleDb);
int i = olc.ExecuteNonQuery();
string a = "select * from 表1";
OleDbDataAdapter oldd = new OleDbDataAdapter(a, oleDb);
DataTable dt = new DataTable();
oldd.Fill(dt);
foreach (DataRow item in dt.Rows)
Show(item);
Console.WriteLine("--------------------------------------------\n");
return i > 0;
}
public bool update()
{
string sql = "update 表1 set 分数=20,宿舍='666' where 姓名='wzq' ";
OleDbCommand oldc = new OleDbCommand(sql, oleDb);
int i = oldc.ExecuteNonQuery();
string a = "select * from 表1";
OleDbDataAdapter oldd = new OleDbDataAdapter(a, oleDb);
DataTable dt = new DataTable();
oldd.Fill(dt);
foreach (DataRow item in dt.Rows)
Show(item);
return i > 0;
}
}
class Program
{
static void Main(string[] args)
{
Access a = new Access();
a.Get();
a.Find();
a.Add();
a.Del();
a.update();
Console.ReadKey();
}
}
}
C# 链接 Access数据库
猜你喜欢
转载自blog.csdn.net/lesileqin/article/details/102992819
今日推荐
周排行