c#查询access数据库代码

jieailian 2009-11-01 09:26:08
请问怎样在c#项目中查询access数据库中的数据,代码是怎样啊??请高手指点
...全文
758 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
axibawocha 2009-11-03
  • 打赏
  • 举报
回复
select * from user
select * from uuser
select * from uuuser
select * from uuuuser
select * from uuuuuser
select * from uuuuuuser
关键字!!!!!!!!!!!!
gdlpc 2009-11-02
  • 打赏
  • 举报
回复
我正在使用的:
using System.Data.OleDb;

protected void Page_Load(object sender, EventArgs e)
{//数据库是在App_Data下的studentinfo.mdb
string tabel_name = "test";//数据表名称
OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OleDb.4.0;Data Source=|DataDirectory|\\studentinfo.mdb");
conn.Open();
OleDbDataAdapter da = new OleDbDataAdapter("select * from "+tabel_name+" order by id asc", conn);//desc降序 asc升序
OleDbCommandBuilder cmd = new OleDbCommandBuilder(da);
DataSet ds = new DataSet();
da.Fill(ds, "basic_info");
GridView1.DataSource = ds;
GridView1.DataBind();
conn.Close();
}

我的测试站:http://nz.oicp.net:808
jieailian 2009-11-02
  • 打赏
  • 举报
回复
请大家看看我这个窗体的代码,给我指点一下。还是From子句错误

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace moneySYS
{
public partial class frmLogin : Form
{
private string u_name; //获取用户名
private string u_pwd; //获取密码
private string u_remarks; //获取权限
public frmLogin()
{
InitializeComponent();
}

private void frmLogin_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“moneyDataSet.user”中。您可以根据需要移动或移除它。
this.userTableAdapter.Fill(this.moneyDataSet.user);
}

private void enter_Click(object sender, EventArgs e)
{
string reportPath = Application.StartupPath.Substring(0, Application.StartupPath.Substring(0, Application.StartupPath.LastIndexOf("\\")).LastIndexOf("\\"));
reportPath += @"\\money.mdb";
string ConStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + reportPath;
//创建OLEDB连接对象
OleDbConnection con = new OleDbConnection(ConStr);
con.Open();
if (con.State == ConnectionState.Open)
{
OleDbCommand ole = new OleDbCommand("select pwd from user where uname='"+name.Text+"'");


welcome wle = new welcome();
this.Hide();
wle.Show();

}

}

private void exit_Click(object sender, EventArgs e)
{
Application.Exit();
}

private void name_SelectedIndexChanged(object sender, EventArgs e)
{
string reportPath = Application.StartupPath.Substring(0, Application.StartupPath.Substring(0, Application.StartupPath.LastIndexOf("\\")).LastIndexOf("\\"));
reportPath += @"\\money.mdb";
string ConStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + reportPath;
//创建OLEDB连接对象
OleDbConnection con = new OleDbConnection(ConStr);
con.Open();
OleDbCommand cmd = new OleDbCommand("select * from user where uname=+'"+ this.name.Text +"'+",con);
OleDbDataReader adr = cmd.ExecuteReader();
if (adr.Read())
{
this.remarks.Text = adr["rmks"].ToString();
u_remarks = this.remarks.Text;

}

}

}
}
jieailian 2009-11-02
  • 打赏
  • 举报
回复
我像11楼那样试了,还是不行啊,把+去掉也不行,请高手指点。。。感激不尽
hhc123 2009-11-02
  • 打赏
  • 举报
回复
("select * from user where uname= +'" + this.name.Text + "'+", con);
再不行把两个红色+去掉,'" + this.name.Text + "' 一个要单引号
hhc123 2009-11-02
  • 打赏
  • 举报
回复
("select * from user where uname=" + this.name.Text + "", con);
有错误
写成这样子就行了
("select * from user where uname= +'" + this.name.Text + "'+", con);
jieailian 2009-11-02
  • 打赏
  • 举报
回复
private void name_SelectedIndexChanged(object sender, EventArgs e)
{
string reportPath = Application.StartupPath.Substring(0, Application.StartupPath.Substring(0, Application.StartupPath.LastIndexOf("\\")).LastIndexOf("\\"));
reportPath += @"\\money.mdb";
string ConStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + reportPath;
//创建OLEDB连接对象
OleDbConnection con = new OleDbConnection(ConStr);
con.Open();
OleDbCommand cmd = new OleDbCommand("select * from user where uname=" + this.name.Text + "", con);
OleDbDataReader adr = cmd.ExecuteReader();
if (adr.Read())
{
this.remarks.Text = adr.GetValue(0).ToString();
u_remarks = this.remarks.Text;

}

我的选择下拉表代码如上,为什么运行后我选择后会说FROM子句语法错误啊。。
我的目的是想获取管理员权限。。。请高手指点。谢谢
sinpoal 2009-11-02
  • 打赏
  • 举报
回复
OleDbConnection cnn=new OleDbConnection(Provider=Microsoft.Jet.OleDb.4.0;
Data Source=MapPath("数据库名.mdb));
cnn.open();
Oledbcommand cmm=new Oledbcommand();
cmm.connection=cnn;
cmm.commandtype=commandtype.text;
cmm.commandtext="SELECT * FROM 表名";
OleDbDataReader adr = Cmm.ExecuteReader();

while(adr.Read())
{

控件.text = adr["字段名"].ToString();
控件.text= adr["字段名"].ToString();

}

cnn.close();
看看这个方法。
SQL77 2009-11-02
  • 打赏
  • 举报
回复
private void Form1_Load(object sender, EventArgs e) 
{
string datacon = global::accesscon.Properties.Settings.Default.db1ConnectionString1;
string sqldatacon = "select count(*) from stu_info ";
OleDbConnection olecon = new OleDbConnection(datacon);
olecon.Open();
OleDbCommand olecom = new OleDbCommand(sqldatacon, olecon);
textBox1.Text = olecom.ExecuteScalar().ToString(); ;
}
flyerwing 2009-11-02
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 wuyq11 的回复:]
用access操作类
public static string connectionString = "";
public static OleDbDataReader ExecuteReader(string strSQL)   
        {   
            OleDbConnection connection = new OleDbConnection(connectionString);   
            OleDbCommand cmd = new OleDbCommand(strSQL, connection);   
            try 
            {   
                connection.Open();   
                OleDbDataReader myReader = cmd.ExecuteReader();   
                return myReader;   
            }   
            catch (System.Data.OleDb.OleDbException e)   
            {   
                throw new Exception(e.Message);   
            }   
        }   
public static DataSet Query(string SQLString)   
        {   
            using (OleDbConnection connection = new OleDbConnection(connectionString))   
            {   
                DataSet ds = new DataSet();   
                try 
                {   
                    connection.Open();   
                    OleDbDataAdapter command = new OleDbDataAdapter(SQLString, connection);   
                    command.Fill(ds, "ds");   
                }   
                catch (System.Data.OleDb.OleDbException ex)   
                {   
                    throw new Exception(ex.Message);   
                }   
                return ds;   
            }   
        }   

[/Quote]
这个长顶这个
vssvss 2009-11-02
  • 打赏
  • 举报
回复
参考以下代码,看能否解决到你的问题

string strConnection="Provider=Microsoft.Jet.OleDb.4.0;";
strConnection+=@"Data Source=MapPath("数据库.mdb");//这里是相对路径

OleDbConnection objConnection=new OleDbConnection(strConnection);
objConnection.open();

string  strSQL="SELECT  *  FROM  Table";
OleDbCommand Comm = new OleDbCommand(strSQL, objConnection);

OleDbDataReader adr = Comm.ExecuteReader();

  while(adr.Read())
  {
             
    变量 = adr.GetValue(0).ToString();
    变量 = adr.GetValue(1).ToString();
.
.              
  }

objConnection.close();
wuyq11 2009-11-02
  • 打赏
  • 举报
回复
用access操作类
public static string connectionString = "";
public static OleDbDataReader ExecuteReader(string strSQL)
{
OleDbConnection connection = new OleDbConnection(connectionString);
OleDbCommand cmd = new OleDbCommand(strSQL, connection);
try
{
connection.Open();
OleDbDataReader myReader = cmd.ExecuteReader();
return myReader;
}
catch (System.Data.OleDb.OleDbException e)
{
throw new Exception(e.Message);
}
}
public static DataSet Query(string SQLString)
{
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
DataSet ds = new DataSet();
try
{
connection.Open();
OleDbDataAdapter command = new OleDbDataAdapter(SQLString, connection);
command.Fill(ds, "ds");
}
catch (System.Data.OleDb.OleDbException ex)
{
throw new Exception(ex.Message);
}
return ds;
}
}
huyidao007 2009-11-02
  • 打赏
  • 举报
回复
<add name="xkConnStr" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\XKArticle.mdb" providerName="System.Data.OleDb"/>
Gmjinrong 2009-11-02
  • 打赏
  • 举报
回复
参考以下代码,看能否解决到你的问题

string strConnection="Provider=Microsoft.Jet.OleDb.4.0;";
strConnection+=@"Data Source=MapPath("数据库.mdb");//这里是相对路径

OleDbConnection objConnection=new OleDbConnection(strConnection);
objConnection.open();

string strSQL="SELECT * FROM Table";
OleDbCommand Comm = new OleDbCommand(strSQL, objConnection);

OleDbDataReader adr = Comm.ExecuteReader();

while(adr.Read())
{

变量 = adr["字段名"].ToString();
变量 = adr["字段名"].ToString();

}

objConnection.close();
realOffar 2009-11-01
  • 打赏
  • 举报
回复
同楼上
freeboy827 2009-11-01
  • 打赏
  • 举报
回复
oledbconnection

110,537

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧