Tuesday, June 18, 2013

Latihan Form


matkul manager


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace koneksi
{
    class MataKuliahManager
    {
        public int add(MataKuliah mk)
        {
            string query;

            if (mk.Prasyarat.ToString() == "" + "values('" + mk.Kode + "','" + mk.Nama +"','" + mk.Sks + "',null,"+
                ""+mk.Semester+")";
        }
        else
    {
        query = "insert into matakuliah " + "values('" + mk.Kode + "','" + mk.Nama + "'," + "" + mk.Sks + ",'" + mk.Prasyarat + "'," + "" + mk.Semester + ")";
    }

    try
    {
        interface x=Fungsi.EQuery(query);
            return x;
        }
    catch (Exception extern)
{
    return 0;
           }
        }
    }





matkul


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace koneksi
{
    class MataKuliah
    {
        string kode, nama, prasyarat;

        public string Prasyarat
        {
            get { return prasyarat; }
            set { prasyarat = value; }
        }
        public string Nama
        {
            get { return nama; }
            set { nama = value; }
        }
        public string Kode
        {
            get { return kode; }
            set { kode = value; }
        }
        int sks, semester;

        public int Semester
        {
            get { return semester; }
            set { semester = value; }
        }

        public int Sks
        {
            get { return sks; }
            set { sks = value; }
        }

        //Constructor
        public MataKuliah(string kode, string nama, int sks, string prasyarat, int semester)
        {
            this.kode = kode;
            this.nama = nama;
            this.sks = sks;
            this.prasyarat = prasyarat;
            this.semester = semester;
        }
    }
}


Fungsi

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace koneksi
{
    class Fungsi
    {
        static SqlConnection conn;
        static SqlCommand cmd;

        public static int EQuery(string query)
        {
            conn = Koneksi.Conn;
            cmd = new SqlCommand();
            try
            {
                conn.Open();
                cmd.Connection = conn;
                cmd.CommandText = query;

                int i = cmd.ExecuteNonQuery();
                return i;
            }
            catch (SqlException ex)
            {
                Console.WriteLine(ex.ToString());
                return 0;
            }
            finally
            {
                conn.Close();
            }
        }
    }
}


koneksi

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace koneksi
{
    class Koneksi
    {
        static string conn;

        public static SqlConnection Conn
        {
            get
            {
                return new SqlConnection(conn);
            }
        }

        static Koneksi()
        {
            string connStr = "server=localhost; " + "database=akademik_0615128401; User id=sa; " + "Password=rahasia";
            conn = connStr;
        }
    }
}


form1



No comments: