Die Constraints macht Probleme bei der Fill-Methode



  • Guten liebe Programmierer-Welt

    Wollte es mal programmier-technisch versuchen ein DataSet mittels OleDbAdapter zu befüllen, doch die DS-Constraints macht mir immer einen Strich durch die Rechnung.

    Kann mir Jemand unter die Arme greiffen beim Lösen dieses Problems?

    Ich poste mal meinen Code

    #using <mscorlib.dll>
    #using <System.dll>
    
    namespace DatenbankKomplett {
    
    	using namespace System;
    	using namespace System::ComponentModel;
    	using namespace System::Collections;
    	using namespace System::Windows::Forms;
    	using namespace System::Data;
    	using namespace System::Data::OleDb;
    	using namespace System::Data::SqlTypes;
    	using namespace System::Drawing;
    
    	/// <summary>
    	/// Zusammenfassung für MyForm
    	/// </summary>
    	public ref class MyForm : public System::Windows::Forms::Form
    	{
    	public:
    		MyForm(void)
    		{
    			InitializeComponent();
    			//
    			//TODO: Konstruktorcode hier hinzufügen.
    			//
    		}
    
    	protected:
    		/// <summary>
    		/// Verwendete Ressourcen bereinigen.
    		/// </summary>
    		~MyForm()
    		{
    			if (components)
    			{
    				delete components;
    			}
    		}
    	private: System::Windows::Forms::Button^  btnLoad;
    	private: System::Windows::Forms::Button^  btnSchliessen;
    	private: System::Windows::Forms::ListView^  lvw;
    	private: System::Windows::Forms::Label^  lbl;
    	protected:
    
    	protected:
    
    	private:
    		/// <summary>
    		/// Erforderliche Designervariable.
    		/// </summary>
    		System::ComponentModel::Container ^components;
    
    #pragma region Windows Form Designer generated code
    		/// <summary>
    		/// Erforderliche Methode für die Designerunterstützung.
    		/// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden.
    		/// </summary>
    		void InitializeComponent(void)
    		{
    			this->btnLoad = (gcnew System::Windows::Forms::Button());
    			this->btnSchliessen = (gcnew System::Windows::Forms::Button());
    			this->lvw = (gcnew System::Windows::Forms::ListView());
    			this->lbl = (gcnew System::Windows::Forms::Label());
    			this->SuspendLayout();
    			// 
    			// btnLoad
    			// 
    			this->btnLoad->Location = System::Drawing::Point(42, 37);
    			this->btnLoad->Name = L"btnLoad";
    			this->btnLoad->Size = System::Drawing::Size(116, 37);
    			this->btnLoad->TabIndex = 0;
    			this->btnLoad->Text = L"Load";
    			this->btnLoad->UseVisualStyleBackColor = true;
    			this->btnLoad->Click += gcnew System::EventHandler(this, &MyForm::btnLoad_Click);
    			// 
    			// btnSchliessen
    			// 
    			this->btnSchliessen->Location = System::Drawing::Point(1022, 576);
    			this->btnSchliessen->Name = L"btnSchliessen";
    			this->btnSchliessen->Size = System::Drawing::Size(116, 37);
    			this->btnSchliessen->TabIndex = 1;
    			this->btnSchliessen->Text = L"Schliessen";
    			this->btnSchliessen->UseVisualStyleBackColor = true;
    			this->btnSchliessen->Click += gcnew System::EventHandler(this, &MyForm::btnSchliessen_Click);
    			// 
    			// lvw
    			// 
    			this->lvw->FullRowSelect = true;
    			this->lvw->GridLines = true;
    			this->lvw->Location = System::Drawing::Point(42, 147);
    			this->lvw->MultiSelect = false;
    			this->lvw->Name = L"lvw";
    			this->lvw->Size = System::Drawing::Size(640, 353);
    			this->lvw->TabIndex = 2;
    			this->lvw->UseCompatibleStateImageBehavior = false;
    			this->lvw->View = System::Windows::Forms::View::Details;
    			// 
    			// lbl
    			// 
    			this->lbl->AutoSize = true;
    			this->lbl->Location = System::Drawing::Point(39, 526);
    			this->lbl->Name = L"lbl";
    			this->lbl->Size = System::Drawing::Size(46, 17);
    			this->lbl->TabIndex = 3;
    			this->lbl->Text = L"label1";
    			// 
    			// MyForm
    			// 
    			this->AutoScaleDimensions = System::Drawing::SizeF(8, 16);
    			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
    			this->ClientSize = System::Drawing::Size(1171, 638);
    			this->Controls->Add(this->lbl);
    			this->Controls->Add(this->lvw);
    			this->Controls->Add(this->btnSchliessen);
    			this->Controls->Add(this->btnLoad);
    			this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::FixedDialog;
    			this->MaximizeBox = false;
    			this->MinimizeBox = false;
    			this->Name = L"MyForm";
    			this->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen;
    			this->Text = L"MyForm";
    			this->ResumeLayout(false);
    			this->PerformLayout();
    
    		}
    #pragma endregion
    	private: System::Void btnLoad_Click(System::Object^  sender, System::EventArgs^  e) {
    
    		this->btnLoad->Enabled = false;
    
    		OleDbConnection^ conn = gcnew OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=F:\\DB\\DB.accdb");
    		OleDbDataAdapter^ adapter = gcnew OleDbDataAdapter();
    
    		DataTable^ dt = gcnew DataTable("table");
    
    		try
    		{
    
    			DataColumn^ colPrimaryKey = gcnew DataColumn("colID");
    			colPrimaryKey->DataType = System::Type::GetType("System.Int32");
    			colPrimaryKey->AllowDBNull = false;
    			colPrimaryKey->AutoIncrement = true;
    			colPrimaryKey->AutoIncrementSeed = 0;
    			colPrimaryKey->AutoIncrementStep = 1;
    			colPrimaryKey->Caption = "ID";
    			colPrimaryKey->MaxLength = -1;
    			colPrimaryKey->Unique = true;
    			colPrimaryKey->ReadOnly = true;
    			dt->Columns->Add(colPrimaryKey);
    
    			DataColumn^ colDaemung = gcnew DataColumn("colErnte");
    			colDaemung->DataType = System::Type::GetType("System.String");
    			colDaemung->AllowDBNull = false;
    			colDaemung->AutoIncrement = false;
    			colDaemung->Caption = "Ernte";
    			colDaemung->MaxLength = 100;
    			colDaemung->Unique = false;
    			colDaemung->ReadOnly = true;
    			dt->Columns->Add(colDaemung);
    
    			DataColumn^ colLage = gcnew DataColumn("colGewicht");
    			colLage->DataType = System::Type::GetType("System.Double");
    			colLage->AllowDBNull = false;
    			colLage->AutoIncrement = false;
    			colLage->Caption = "Gewicht";
    			colLage->MaxLength = 100;
    			colLage->Unique = false;
    			colLage->ReadOnly = true;
    			dt->Columns->Add(colLage);
    
    			DataColumn^ colMoney = gcnew DataColumn("colMoney");
    			colMoney->DataType = System::Type::GetType("System.Decimal");
    			colMoney->AllowDBNull = false;
    			colMoney->AutoIncrement = false;
    			colMoney->Caption = "Preis m2";
    			colMoney->MaxLength = -1;
    			colMoney->Unique = false;
    			colMoney->ReadOnly = true;
    			dt->Columns->Add(colMoney);
    
    			array<DataColumn^>^ colsdata = { colPrimaryKey };
    
    			for (int i = 0; i < colsdata->Length -1; i++)
    			{
    				dt->PrimaryKey[i] = dt->Columns["colID"];
    			}
    
    			DataSet^ ds = gcnew DataSet();
    			ds->DataSetName = "dataset";
    
    			ds->Tables->Add(dt);
    
    			adapter->MissingSchemaAction = MissingSchemaAction::AddWithKey;
    			conn->Open();
    			adapter->SelectCommand = gcnew OleDbCommand("SELECT ID, Ernte, Gewicht, Preis\r\nFROM     Erdbeeren", conn);
    
    			adapter->Fill(ds,"table"); // Löst Fehler aus;
    
    catch (OleDbException^ oleDbEx)
    		{
    			MessageBox::Show(oleDbEx->Message->ToString()+ "\n\n"+ oleDbEx->StackTrace->ToString());
    		}
    		catch (DuplicateNameException^ dupEx)
    		{
    			MessageBox::Show(dupEx->Message->ToString() + "\n\n" + dupEx->StackTrace->ToString());
    		}
    		catch (ConstraintException^ constrEx)
    		{
    			MessageBox::Show(constrEx->Message->ToString() + "\n\n" + constrEx->StackTrace->ToString() +"\n\n" + constrEx->Source->ToString());
    		}
    		catch (NullReferenceException^ nullrefEx)
    		{
    			MessageBox::Show(nullrefEx->Message->ToString() + "\n\n" + nullrefEx->StackTrace->ToString());
    		}
    		catch (ArgumentException^ ArgEx)
    		{
    			MessageBox::Show(ArgEx->Message->ToString() + "\n\n" + ArgEx->StackTrace->ToString());
    		}
    		catch (Exception^ er)
    		{
    			MessageBox::Show(er->Message->ToString() + "\n\n" + er->StackTrace->ToString());
    		}
    		finally
    		{
    			if (conn->State == ConnectionState::Open)
    			{
    				conn->Close();
    				delete (IDisposable^)conn;
    				delete (IDisposable^)adapter;
    			}
    		}
    
    	}
    

    Folgend die Fehlermeldung:

    Einschränkungen konnten nicht aktiviert werden. Mindestens eine Zeile enthält Werte die die Einschränkungen non-null, unique or foreign-key verletzen.

    bei System.Data.DataSet.EnableConstraints()
    bei System.Data.DataSet.set_EnforceConstraints(Boolean value)
    bei System.Data.DataTable.EndLoadData()
    bei System.Data.Common.DataAdapter.FillFromReader(DataSet dataset, DataTable datatable, String srcTable, DataReaderContainer dataReader, Int32 startRecord, Int32 maxRecords, DataColumn parentChapterColumn, Object parentChapterValue)
    bei System.Data.Common.DataAdapter.Fill(DataSet dataSet, String srcTable, IDataReader dataReader, Int32 startRecord, Int32 maxRecords)
    bei System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
    bei System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
    bei System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, String srcTable)
    bei DatenbankKomplett.MyForm.btnLoad_Click(Object sender, EventArgs e) in f:\projekte\datenbankkomplett\datenbankkomplett\myform.h:Zeile 209.

    System.Data

    Vielen Dank im Voraus für Euer Engagement ..

    Lieben Gruss


Anmelden zum Antworten