I am trying to fill a datagridview with content from a .mdf SQL Server database file (C# Windows Forms app)...

private void Companies_Load(object sender, EventArgs e)
{
   load_table();
}

void load_table()
{
            String DATA = Application.StartupPath + @"\data.mdf";
            string constring = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=" + DATA + ";Integrated Security=True";

            MySqlConnection conDataBase = new MySqlConnection(constring);
            MySqlCommand cmdDataBase = new MySqlCommand("select * from Companies ;", conDataBase);
            try
            {
                MySqlDataAdapter sda = new MySqlDataAdapter();
                sda.SelectCommand = cmdDataBase;
                DataTable dbdataset = new DataTable();
                sda.Fill(dbdataset);
                BindingSource bSource = new BindingSource();

                bSource.DataSource = dbdataset;
                dataGridView1.DataSource = bSource;
                sda.Update(dbdataset);
            }
            catch (Exception uu)
            {
                MessageBox.Show(uu.Message);
            }
        }

I get nothing. DataGridView is empty. No errors...

Table name: Companies with 4 rows and 1 column...

I tried SQL statements like

select * from dbo.Companies ;

... still nothing

I changed data.mdf connection to full path c:/etc/etc ...

No luck.

Any simple solution is welcome :)

有帮助吗?

解决方案

.mdf is a SQL Server data file, therefore you need to use the SQL Server client library, e.g. SqlConnection, SqlCommand and SqlDataAdapter.

What you're using now (MySqlConnection, MySqlCommand, MySqlDataAdapter) is for MySQL and won't work on a (Microsoft) SQL Server data file.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top