Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

C# Datatable wird nicht geschrieben.

Empfohlene Antworten

Veröffentlicht

Hallo,

z.Z. versuche ich eine xls-Datei in eine mdb zu kopieren.

Die xls wird zwar wunderbar eingelesen und so, aber das schreiben in die mdb funktioniert nicht. (Es wird zwar kein Fehler ausgegeben, aber die Datensätze stehen trotzdem nicht drin.)

Die Oberfläche besteht aus einem DataGridView (dgv) und 2 Buttons (für den Import der xls und den Export in die mdb).

Wie gesagt laden klappt soweit ganz gut:


private void btnImport_Click(object sender, EventArgs e)

        {

            this.openFileDialog1.FileName = "*.xls";


            if (this.openFileDialog1.ShowDialog() == DialogResult.OK)

            {

                xlsmdbConnection conn = new xlsmdbConnection(openFileDialog1.FileName, "Extended Properties=Excel 8.0;");

                dgv.DataSource = conn.readDataSet("SELECT * FROM [DB$]").Tables[0];

            }

        }

Nur schreiben nicht:

        private void btnExportmdb_Click(object sender, EventArgs e)

        {

            this.openFileDialog1.FileName = "*.mdb";


            if (this.openFileDialog1.ShowDialog() == DialogResult.OK)

            {

                xlsmdbConnection conn = new xlsmdbConnection(openFileDialog1.FileName, "");

                dgv.EndEdit();

                conn.writeDataSet((System.Data.DataTable)dgv.DataSource, "LoadCarryingCapacity");

            }

        }

Die Funktion xlsmdbConnection.writeDataSet sieht wie folgt aus:

        public void writeDataSet(DataTable datatable, string table)

        {

            datatable.TableName = table;

            try

            {

                conn.Open();

                OleDbDataAdapter da = new OleDbDataAdapter();

                OleDbCommand cmd = new OleDbCommand("INSERT INTO " + table + "(HitMainTypeID, HitSubTypeID, LoadRangeID, Height, M_Plus, M_Minus, V_Plus, V_Minus, Interaction, ConcreteCover, Concrete, ShearForceID) " +

                       "VALUES (@HitMainTypeID, @HitSubTypeID, @LoadRangeID, @Height, @M_Plus, @M_Minus, @V_Plus, @V_Minus, @Interaction, @ConcreteCover, @Concrete, @ShearForceID)", conn);


                cmd.Parameters.Add("@HitMainTypeID", OleDbType.VarChar, 10, "HitMainTypeID");

                cmd.Parameters.Add("@HitSubTypeID", OleDbType.VarChar, 10, "HitSubTypeID");

                cmd.Parameters.Add("@LoadRangeID", OleDbType.VarChar, 10, "LoadRangeID");

                cmd.Parameters.Add("@Height", OleDbType.Integer, 10, "Height");

                cmd.Parameters.Add("@M_Plus", OleDbType.Double, 10, "M_Plus");

                cmd.Parameters.Add("@M_Minus", OleDbType.Double, 10, "M_Minus");

                cmd.Parameters.Add("@V_Plus", OleDbType.Double, 10, "V_Plus");

                cmd.Parameters.Add("@V_Minus", OleDbType.Double, 10, "V_Minus");

                cmd.Parameters.Add("@Interaction", OleDbType.Integer, 10, "Interaction");

                cmd.Parameters.Add("@ConcreteCover", OleDbType.Integer, 10, "ConcreteCover");

                cmd.Parameters.Add("@Concrete", OleDbType.VarChar, 50, "Concrete");

                cmd.Parameters.Add("@ShearForceID", OleDbType.VarChar, 10, "ShearForceID");


                da.InsertCommand = cmd;


                da.Update(datatable);

            }

            catch (Exception e)

            {

                Console.Write("xlsConnection:readDataSet:\t{0}\n", e);

            }

            finally

            {

                conn.Close();

            }

        }

Da nichtmal eine Exception geworfen wird, macht mich das schon etwas stutzig und ich wäre dankbar für Hilfe.

hmm mich würde mal interessieren wie du die Verbindung zur Datenbank herstellst...

nur conn.open() ???

Wie sieht denn der ConnectionString aus??

mfg

MSC

  • Autor

Also erstmal: Die Klasse xlsmdbConnection wurde jetzt umbenannt in DbReader.

Diese sieht an sich wie folgt aus:

private const string connectionString0 = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source="; 

        private const string connectionString1 = ";"; 

        private string path = "";

        private string exProperties = "";


        private OleDbConnection conn = null;


        public DbReader(string path, string exProperties)

        {

            try

            {

                this.path = path;

                this.exProperties = exProperties;

                init();

            }

            catch (Exception e)

            {

                Console.Write("DbReader:DbReader:\t{0}\n", e);

            }

        }


        private string getConnectionString()

        {

            return connectionString0 + path + connectionString1 + exProperties;

        }


        private void init()

        {

            this.conn = new OleDbConnection(getConnectionString());

        }

gelesen wird so:

public DataSet readDataSet(string sql)

        {

            DataSet ret = new DataSet();

            try

            {

                conn.Open();

                OleDbCommand cmd = new OleDbCommand(sql, conn);

                OleDbDataAdapter da = new OleDbDataAdapter();

                da.SelectCommand = cmd;

                da.Fill(ret);

            }

            catch (Exception e)

            {

                Console.Write("DbReader:readDataSet:\t{0}\n", e);

            }

            finally

            {

                conn.Close();

            }

            return ret;

        }

Meine Vermutung: Das DataSet wird nicht geschrieben, weil keine Veränderungen in der Objectlist entsteht und der bei DataAdapter.update (so glaube ich) nur veränderte Werte schreibt.

Gibts eine Möglichkeit das zu umgehen oder muss ich eine Iteration (row, col, value) nehmen?

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.