使用C#连接access数据库 读取某一行的数据

在数据库建立一个叫A的表 里面的列名有id XX XXX XXXX 想通过指定ID的值读取这一行的数据,怎么做??谢谢各位

建立FORM窗体,加个按钮控件,加一个DATAGRIDVIEW控件,双击FORM,加入命名空间using System.Data.OleDb; 双击按钮,进入按钮代码。

    OleDbConnectionstrConnectionnewOleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + "员工信息.mdb" + ";Persist Security Info=False")。

2. //建立数据库引擎连接,注意数据表(后缀为.db)应放在DEBUG文件下。

3.OleDbDataAdapter myda = new OleDbDataAdapter("select * from 。

4.trConnection);   //建立适配器,通过SQL语句去搜索数据库。

5.按F5运行后,点击BUTTON按钮,便会显示相应的SQL语句下的数据库里的表。

6. string strcon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db4.mdb;";     mycon = new OleDbConnection(strcon)。

温馨提示:答案为网友推荐,仅供参考
第1个回答  2018-01-21
string path = Directory.GetCurrentDirectory() + "\\Access\\" + name + ".mdb";//数据库路径
string ConStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path;
OleDbConnection myconn = new OleDbConnection(ConStr);
myconn.Open();//打开数据库
string strSql = "select * from A where code = '" + ID + "'";//要查询的ID
OleDbCommand cmd = new OleDbCommand(strSql, myconn);
DataSet ds = new DataSet();
OleDbDataAdapter da = new OleDbDataAdapter();
da.SelectCommand = cmd;
da.Fill(ds);
DataRow dr = ds.Tables[0].Rows[0];//获取所查询的那一行的信息
string[] strr = new string[2];
strr[0] = dr["字段1"].ToString();
strr[1] = dr["字段2"].ToString();
listView1.Items.Clear();
ListViewItem lv = new ListViewItem(strr);//显示到listview控件中(自己选择要显示的控件)
listView1.Items.Add(lv);
第2个回答  推荐于2018-01-21
一、建立FORM窗体,加一个按钮控件,加一个DATAGRIDVIEW控件。
二、双击FORM,加入命名空间using System.Data.OleDb;
双击按钮,进入按钮代码,写如下代码
OleDbConnection strConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + "员工信息.mdb" + ";Persist Security Info=False");
//建立数据库引擎连接,注意数据表(后缀为.db)应放在DEBUG文件下
OleDbDataAdapter myda = new OleDbDataAdapter("select * from 雇员 ,strConnection);
//建立适配器,通过SQL语句去搜索数据库
DataSet myds = new DataSet();
//建立数据集
myda.Fill(myds, "雇员");
//用FILL的方式将适配器已经连接好的数据表填充到数据集MYDS这张表
dataGridView1.DataSource = myds.Tables["联系人ID"];
//用显示控件来显示表
三、按F5运行后,点击BUTTON按钮,便会显示相应的SQL语句下的数据库里的表。

下面利用Command和reader对象在控制台应用程序下输出数据。
[csharp] view plain copy print?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;

namespace ConsoleApplication19
{
class Program
{
static void Main(string[] args)
{

OleDbConnection mycon =null;
OleDbDataReader myReader=null;
try
{
string strcon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db4.mdb;";
mycon = new OleDbConnection(strcon);
mycon.Open();
string sql = "select * from 雇员 ";
OleDbCommand mycom = new OleDbCommand(sql, mycon);
myReader = mycom.ExecuteReader();
while (myReader.Read())
{
Console.WriteLine(myReader.GetString(0)+" "+myReader.GetDouble(1)+" "+myReader.GetString(2)+" "+myReader.GetString(3)+" "+myReader.GetString(4));

}

}
finally
{
myReader.Close();
mycon.Close();

}
}
}
}本回答被提问者采纳
相似回答