private void button1_Click(object sender, EventArgs e)
        {
            string strConnection = "Provider=Microsoft.Jet.OleDb.4.0;";
            strConnection += @"Data SourceD:\NET\TEST\Data.mdb";
            string strSQL = "SELECT * FROM dl";
            OleDbConnection myConn = new OleDbConnection(strConnection);     初始化字符串的格式不符合规范。
            OleDbCommand myCmd = new OleDbCommand(strSQL, myConn);
            OleDbDataReader datareader = null;
            try
            {
                myConn.Open();
                datareader = myCmd.ExecuteReader();
                while (datareader.Read())
                {
                    myConn.Open();    
                    datareader = myCmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
                    this.dataGridView1.DataSource = datareader;
                        //    this.dataGridView1.DataBindings();                                          datareader.Close();
                    myCmd.Dispose();
                    myConn.Dispose();
                    myConn = null;                }
            }
            catch (Exception e)
            {
                string Messate = e.Message;
            }
            finally
            {
                myConn.Close();
            }哪位大哥知道,谢谢了。。如果有源码贴上来分享一下,本人不胜感激!
BAIDU搜过了!BAIDU的都不是很全!

解决方案 »

  1.   

         OleDbConnection myConn = new OleDbConnection(strConnection);    初始化字符串的格式不符合规范。黄色字是这个:谢谢了!
      

  2.   


      strConnection += @"Data Source=D:\NET\TEST\Data.mdb"; 这样看看
      

  3.   

    strConnection="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=rsDB.mdb;Persist Security Info=False;"; 
      

  4.   

    App.config中这样写:<?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <appSettings>
        <add  key="constr" value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=rsDB.mdb;Persist Security Info=False;" />
      </appSettings>
    </configuration>外部引用时这样写:        private string _constr = System.Configuration.ConfigurationSettings.AppSettings["constr"].ToString();
            public rsclass()
            {
            }
            public rsclass(string constr)
            {
                _constr = constr;
            }
            public string ConStr
            {
                get { return _constr; }
                set { _constr = value; }
            }
            public string queryCompany()
            {
                OleDbConnection Conn = new OleDbConnection(ConStr);
                string SqlStr = "select companyname from tcompany";
                Conn.Open();
                OleDbCommand cmd = new OleDbCommand(SqlStr, Conn);
                string companyname = cmd.ExecuteScalar().ToString();
                Conn.Close();
                Conn.Dispose();
                return companyname;
            }
      

  5.   

     public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            public string ConStr = System.Configuration.ConfigurationSettings.AppSettings["constr"].ToString();
         
            private void button1_Click(object sender, EventArgs e)
            {
                
                OleDbConnection Conn = new OleDbConnection(ConStr);
                string SqlStr = "select * from dl";
                OleDbCommand myCmd = new OleDbCommand(SqlStr, Conn);
                OleDbDataReader datareader = null;
                Conn.Open();
                string companyname = myCmd.ExecuteScalar().ToString();
                this.dataGridView1.DataSource = datareader;
                this.dataGridView1.DataBindings(); 
                Conn.Close();
                Conn.Dispose();
              //  return companyname;
            }
    我改成这样了! public string ConStr = System.Configuration.ConfigurationSettings.AppSettings["constr"].ToString();
      这个报警告
    1 “System.Configuration.ConfigurationSettings.AppSettings”已过时:“This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.AppSettings” D:\NET\showaccess\showaccess\Form1.cs 18 32 showaccess
       this.dataGridView1.DataBindings();  报错误 2 “System.Windows.Forms.Control.DataBindings”是“属性”,但此处被当做“方法”来使用 D:\NET\showaccess\showaccess\Form1.cs 30 32 showaccess
    web.config 我改成了<?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <appSettings>
        <add  key="constr" value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\NET\TEST\Data.mdb;Persist Security Info=False;" />
      </appSettings>
    </configuration>看看可以吗?谢谢!
      

  6.   

    补充,就是点击BUTTOM按钮,DATAGRIDVIEW显示 ACCESS里面的数据!
    谢谢!
      

  7.   

    1.那个过期的警告不用去理他,我这也有同样的警告,但好像还没有新的能替代他的。
    2.this.dataGridView1.DataBindings(); 把其中的DataBindings后面的括号去掉,属性没有括号。
    3.数据库的路径最好用相对路径,这样程序的移植能力才强,否则的话,换了盘符就要改路径。