Update

Marcel G

Mitglied
Hallo,
versuch mich grad an Datenbankverbindungen in C#, leider bekomm ich es nicht so
wirklich hin. Bücher und Quellen im Internet haben leider nicht weitergeholfen.
Hab ne UserForm und ein paar Felder drin, die ich mit ner Databinding befüllt hab.
Nun änder ich die Daten ab und die Änderungen sollen in die DB übernommen werden. Wie funktioniert das? Ich tu doch nicht ein Databinding zuweisen, damit ich später nochmal den UPDATE-Befehl aufbau oder sag welche Spalten mit welchen Werten befüllt werden müssen.

Wie mach ich das denn? (cmdSave_Click()

Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlServerCe; 

namespace QManager
{
    public partial class Client : Form
    {
        private DatabaseConnection oDatabaseConnection;
        private CurrencyManager oCurrencyManager;
        private SqlCeDataAdapter oDataAdapter;
        private DataSet oDataSet;
        private DataView oDataView;

        public Client()
        {
	        new Client(-1);
        }

        public Client(int clientNumber)
        {
            InitializeComponent();
            this.oDatabaseConnection = new DatabaseConnection(Properties.Settings.Default.qdataConnectionString + MainWindow.password);
            this.createDatabindings(clientNumber);
            this.loadClient(clientNumber);
        }

        private void Client_Load(object sender, EventArgs e)
        {
        
        }
        
        //private void Client_KeyPress(object sender, KeyPressEventArgs e)
        //{
        //    if(e.KeyChar==(char)27)
        //    {
        //        this.Close();
        //    }
        //}



        public void loadClient(int clientNumber)
        {
        }

        private void createDatabindings(int clientNumber)
        {
            String sSqlCommand = "SELECT * FROM Client";
            if (clientNumber > -1)
            {
                sSqlCommand += " WHERE Client.Id = " + clientNumber;
            }
            SqlCeCommand sqlCommand;

            try
            {
                this.oDatabaseConnection.OpenConnection();

                sqlCommand = new SqlCeCommand(sSqlCommand, this.oDatabaseConnection.getConnection());
                sqlCommand.CommandType = CommandType.Text;

                oDataSet = new DataSet();
                oDataAdapter = new SqlCeDataAdapter(sqlCommand);
                oDataAdapter.Fill(oDataSet);
                SqlCeCommandBuilder cb = new SqlCeCommandBuilder(oDataAdapter);

                this.oDataView = new DataView(oDataSet.Tables[0]);
                oDataView.Table = oDataSet.Tables[0];

                if (oDataSet.Tables[0].Rows.Count > 1)
                {
                    this.cmdNext.Enabled = true;
                    this.cmdPrevious.Enabled = true;
                }
                else
                {
                    this.cmdNext.Enabled = true;
                    this.cmdPrevious.Enabled = true;
                }

                this.oCurrencyManager = (CurrencyManager)this.BindingContext[oDataView];

                this.lblClientNumber.DataBindings.Add("Text", oDataView, "Id");
                this.chkActive.DataBindings.Add("Checked", oDataView, "Activ");
                this.cboSalutation.DataBindings.Add("Text", oDataView, "Gender");
                this.txtTitle.DataBindings.Add("Text", oDataView, "Title");
                this.txtFirstName.DataBindings.Add("Text", oDataView, "FirstName");
                this.txtLastName.DataBindings.Add("Text", oDataView, "LastName");
                this.txtStreet.DataBindings.Add("Text", oDataView, "Street");
                this.txtHouse.DataBindings.Add("Text", oDataView, "House");
                this.txtZip.DataBindings.Add("Text", oDataView, "Zip");
                this.txtCity.DataBindings.Add("Text", oDataView, "City");
                this.txtPhone.DataBindings.Add("Text", oDataView, "Phone1");
                this.txtPhone2.DataBindings.Add("Text", oDataView, "Phone2");
                this.txtMobile.DataBindings.Add("Text", oDataView, "Mobile");
                this.txtFax.DataBindings.Add("Text", oDataView, "Fax");
                this.txtEMail.DataBindings.Add("Text", oDataView, "EMail1");
                this.txtEMail2.DataBindings.Add("Text", oDataView, "EMail2");
                this.txtBirthday.DataBindings.Add("Text", oDataView, "Birthday");
                this.txtNotice.DataBindings.Add("Text", oDataView, "Notice");
                this.cboEmployee1.DataBindings.Add("Text", oDataView, "Employee1");
                this.cboEmployee2.DataBindings.Add("Text", oDataView, "Employee2");
                this.txtAccountNumber.DataBindings.Add("Text", oDataView, "AccountNumber");
                this.txtAccountOwner.DataBindings.Add("Text", oDataView, "AccountOwner");
                this.txtAccountInstitutNumber.DataBindings.Add("Text", oDataView, "AccountInstituteNumber");
                this.txtAccountInstitutName.DataBindings.Add("Text", oDataView, "AccountInstituteName");

                //this.updateLblPosition();
                oDataSet.Dispose();
                oDataAdapter.Dispose();
                sqlCommand.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Exception: " + ex.Message);
            }
            finally
            {
                this.oDatabaseConnection.CloseConnection();
            }
       }

       private void updateTitleInformation()
       {

       }

       private void cmdSave_Click(object sender, EventArgs e)
       {
           
            try
            {
                //was mach ich hier
            }
            catch (Exception ex)
            {
                MessageBox.Show("Exception: " + ex.Message);
            }
            finally
            {
                this.oDatabaseConnection.CloseConnection();
            }
       }

       private void cmdPrevious_Click(object sender, EventArgs e)
       {

           if (oCurrencyManager.Position > 0)
           {
               oCurrencyManager.Position--;
           }
           this.updateTitleInformation();
       }

       private void cmdNext_Click(object sender, EventArgs e)
       {
           if (oCurrencyManager.Position < oCurrencyManager.Count - 1)
           {
               oCurrencyManager.Position++;
           }
           this.updateTitleInformation();
       }

       private void cmdAbort_Click(object sender, EventArgs e)
       {
           this.Close();
       }
    }
}
 
Ich tu doch nicht ein Databinding zuweisen, damit ich später nochmal den UPDATE-Befehl aufbau oder sag welche Spalten mit welchen Werten befüllt werden müssen.

Nicht? .. naja dann viel spass noch... wusste gar nicht das die .Net Entwickler künstliche Intelligenz implementiert hätten
 
Zurück