WPF - Databinding dengan Database

Berikut ini adalah contoh untuk melakukan binding database ke obyek tertentu dengan model standar. 

dbdatabinding

Disini diketikkan kode user dan load, maka hasil dari database akan dibinding ke textbox

Ini contoh source code nya :)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.SqlClient;

namespace DBDataBinding
{
    class TblUserList
    {
        private string _AUserName;
        public string AUserName
        {
            get { return _AUserName; }
            set { _AUserName = value; }
        }
        private string _Name;
        public string Name
        {
            get { return _Name; }
            set { _Name = value; }
        }
        private string _Department;
        public string Department
        {
            get { return _Department; }
            set { _Department = value; }
        }
        public TblUserList()
        {
        }
        public void Load()
        {
            if (_AUserName.Trim().Length == 0)
                throw new Exception("Kode User Tidak ditemukan");
            SqlConnection oConnection = new SqlConnection
            ("Data Source=SQLServer;Initial Catalog=Access_App;user id=suparman");
            oConnection.Open();
 
            string str = "";
            str += "SELECT * ";
            str += "FROM TblUserList ";
            str += "WHERE AUserName = @AUserName";

            SqlCommand oCommand = new SqlCommand(str, oConnection);
            oCommand.CommandType= System.Data.CommandType.Text;
            oCommand.Parameters.Add(new SqlParameter("@AUserName", _AUserName));
            SqlDataReader oDR = oCommand.ExecuteReader();
            if (oDR.Read())
            {
                _Name = oDR["Name"].ToString();
                _Department = oDR["Department"].ToString();
            }
            oCommand.Dispose();
            oConnection.Close();
            oConnection.Dispose();
      }
}
    public partial class Window1 : Window
    {
        TblUserList oTblUserList = new TblUserList();
        public Window1()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
        }
        private void btnLoad_Click(object sender, EventArgs e)
        {
          try
          {
              oTblUserList.AUserName = txtAUserName.Text;
              oTblUserList.Load();
              txtName.Text = oTblUserList.Name;
              txtDepartment.Text = oTblUserList.Department;
          }
          catch (Exception oEx)
          {
             MessageBox.Show(oEx.Message);
          }
        }
 
    }

}

 

Seperti biasa, bagi yang engga sempat ngetik, silahkan download :

 

Share this post: | | | |
Published Monday, October 27, 2008 2:04 PM by Lutfie Royan

Comments

No Comments
Powered by Community Server (Commercial Edition), by Telligent Systems