Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从C查询MS访问_C#_Sql_Ms Access_Ms Access 2007 - Fatal编程技术网

C# 从C查询MS访问

C# 从C查询MS访问,c#,sql,ms-access,ms-access-2007,C#,Sql,Ms Access,Ms Access 2007,我是C语言的新手。我试图查询一个*.accdb数据库,但响应中有0行,datagrid保持清晰。从MS Access进行查询。怎么了 主窗体类 namespace WindowsFormsApplication1 { public partial class Form1 : Form { OleDbConnection database; public Form1() { InitializeCompone

我是C语言的新手。我试图查询一个*.accdb数据库,但响应中有0行,datagrid保持清晰。从MS Access进行查询。怎么了

主窗体类

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        OleDbConnection database;

        public Form1()
        {
            InitializeComponent();
        }

        private void filterButton_Click(object sender, EventArgs e)
        {
            MessageBox.Show(nameFilter.Text);

            InitializeComponent();
            string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\qwe.accdb;";
    try
    {
     database = new OleDbConnection(connectionString);
        database.Open();

        string queryString = "SELECT id FROM table1";
        loadDataGrid(queryString);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
        return;
    }
        }

        public void loadDataGrid(string sqlQueryString) {

            OleDbCommand comm = new OleDbCommand();
            comm.CommandText = sqlQueryString;
            comm.CommandType = CommandType.Text;
            comm.Connection = database;

            Int32 returnValue = comm.ExecuteNonQuery();

            MessageBox.Show(returnValue.ToString());



            OleDbCommand SQLQuery = new OleDbCommand();
            DataTable data = null;
            dataGridView1.DataSource = null;
            SQLQuery.Connection = null;
            OleDbDataAdapter dataAdapter = null;
            dataGridView1.Columns.Clear(); // <-- clear columns

            SQLQuery.CommandText = sqlQueryString;
            SQLQuery.Connection = database;
            data = new DataTable();
            dataAdapter = new OleDbDataAdapter(SQLQuery);
            dataAdapter.Fill(data);
            dataGridView1.DataSource = data;

            MessageBox.Show(data.ToString());

            dataGridView1.AllowUserToAddRows = false; // <-- remove the null line
            dataGridView1.ReadOnly = true;          // <-- so the user cannot type 
        }
    }
}

为什么不做一些大致如下的事情:

private void filterButton_Click(object sender, EventArgs e)
{
    dataGridView1.Columns.Clear();
    MessageBox.Show(nameFilter.Text);
    InitializeComponent();
    string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\qwe.accdb;";
    try
    {
        using (DataTable dt = new DataTable())
            {
                using (OleDbDataAdapter da = new OleDbDataAdapter(new SqlCommand("SELECT id FROM table1",new OleDbConnection(connectionString))))
                {
                    da.Fill(dt);
                    MessageBox.Show(dt.Rows.Count.ToString());
                    dataGridView1.DataSource = dt;
                    dataGridView1.Update();
                }
            }       

    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
        return;
    }
}

您的目标表是Access中的常规表还是链接表?表1是我在Access中创建的表如果我在loadDataGrid方法中看得很清楚,您可以将相同的查询分配给ExecuteOnQuery并填充适配器?