Hello!

1) How to get the number of tables in the database? Their names? And how to work with them in the general case?

For example, when I run an application, I want to get a list of tables in the database, and then click on one of the values ​​to open the table.

At this stage, I work like this:

private void Form1_Load(object sender, EventArgs e) { OleDbConnection connection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=Склад.mdb"); connection.Open(); OleDbCommand command = connection.CreateCommand(); command.CommandText = "SELECT * FROM Товары"; OleDbDataAdapter dataAdapter = new OleDbDataAdapter(); dataAdapter.SelectCommand = command; DataSet ds = new DataSet(); dataAdapter.Fill(ds); dataGridView1.DataSource = ds.Tables[0].DefaultView; connection.Close(); } 

It is clear that I display one table Goods.

How for example to refer to a particular table? Get the names of its columns, their type, etc.? ... This is especially necessary when creating a form for recording data ...

2) Something happened. Through a server browser, I created a connection to my database, and I had an object of type DataSet. Next, after working with him, I achieved the following:

  private void Form1_Load(object sender, EventArgs e) { Sklad mySklad = new Sklad(); //MessageBox.Show("Число таблиц в БД "+mySklad.Tables.Count.ToString()); foreach (DataTable dt in mySklad.Tables) { DataGridView dtGrid = new DataGridView(); tabControl1.TabPages.Add(dt.TableName); dtGrid.Parent = tabControl1.TabPages[tabControl1.TabPages.Count-1]; dtGrid.Dock = DockStyle.Fill; dtGrid.Name = "Table_" + dt.TableName; dtGrid.DataSource = dt.DefaultView; dtGrid.Show(); } } 

But, the tables are empty. Where did the data go? Or how to get them out? I understand that it is necessary through request, but how to interact through request with this object DataSet ...

Thank!

  • if I'm not mistaken, you can drag your tables into the DataSet directly onto the form. - Specter
  • And what will it give? Tell me how to continue to work? - Leshij_2005

2 answers 2

You can work with MS Access as follows:

 OleDbConnection cnn; // ... var tables = cnn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); 

Other values ​​can be passed there. OleDbSchemaGuid

    These commands will help you:

    • SHOW DATABASES;
    • SHOW TABLES FROM имя_базы_данных ;
    • SHOW COLUMNS FROM имя_базы_данных . имя_таблицы ;
    • Nope, they won't help ... I tried) VS swears that only SELECT, INSERT, UPDATE, DELETE, and something else are allowed ... - Leshij_2005
    • This is for which DBMS such commands? - Modus
    • BogolyubskiyAlexey brought commands for MySQL, and I work with Access .. - Leshij_2005
    • I see that neither MS Access nor MS Sql Server support this. - Modus