请问怎样在c#项目中查询access数据库中的数据,代码是怎样啊??请高手指点

解决方案 »

  1.   

    参考以下代码,看能否解决到你的问题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();
      

  2.   

    <add name="xkConnStr" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\XKArticle.mdb" providerName="System.Data.OleDb"/>
      

  3.   

    用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;    
                }    
            }    
      

  4.   

    参考以下代码,看能否解决到你的问题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();
      

  5.   

    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(); ; 
            } 
      

  6.   

    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();
          看看这个方法。
      

  7.   

    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.res.Text = adr.GetValue(0).ToString();
                    u_res = this.res.Text;            }我的选择下拉表代码如上,为什么运行后我选择后会说FROM子句语法错误啊。。
    我的目的是想获取管理员权限请高手指点。谢谢
      

  8.   

    ("select * from user where uname=" + this.name.Text + "", con); 
    有错误
    写成这样子就行了
    ("select * from user where uname= +'" + this.name.Text + "'+", con); 
      

  9.   

    ("select * from user where uname= +'" + this.name.Text + "'+", con); 
    再不行把两个红色+去掉,'" + this.name.Text + "' 一个要单引号
      

  10.   

    请大家看看我这个窗体的代码,给我指点一下。还是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_res;                                   //获取权限
            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.res.Text = adr["rmks"].ToString();
                    u_res = this.res.Text;            }
                
            }
            
        }
    }
      

  11.   

    我正在使用的:
    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
      

  12.   

    select * from user
    select * from uuser
    select * from uuuser
    select * from uuuuser
    select * from uuuuuser
    select * from uuuuuuser
    关键字!!!!!!!!!!!!