C# - Salah satu Koneksi visual C# ke SQL Server 2008, Dengan Store Procedur Dengan Parameter

Koneksi visual C# ke SQL Server 2008, Dengan Store Procedur Dengan Parameter
code #1 koneksi class :





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

namespace Inventory_Sport
{
    public class koneksi
    {
        SqlConnection con;
        SqlCommand cmd;
        SqlDataAdapter da;

        public koneksi ()
        {
            this.con = new SqlConnection(@"uid=sa;pwd=u_password;initial 
catalog=Inventory;data source=.");
        }
        public DataTable execQueryProc(string sql, object[] values)
        {
            this.con.Open();
            this.cmd = new SqlCommand();
            this.cmd.Connection = this.con;

            try
            {
                this.cmd.CommandType = CommandType.StoredProcedure;
                this.cmd.CommandText = sql;

                if (values != null)
                {
                    int prmIndex = 0;

                    SqlCommandBuilder.DeriveParameters(this.cmd);

                    foreach (SqlParameter prm in this.cmd.Parameters)
                    {
                        if (prm.Direction == ParameterDirection.Input)
                        {
                            prm.Value = values[prmIndex];
                            prmIndex++;
                        }
                    }

                }

                this.da = new SqlDataAdapter(this.cmd);

                DataTable dt = new DataTable("data");
                this.da.Fill(dt);

                return dt;

            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                this.cmd.Dispose();
                this.cmd = null;

                if (this.con.State == ConnectionState.Open)
                    this.con.Close();
            }
        }

    }//end class
}

dan pemanggilan di class beda code diatas  code #2 :

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;

namespace Inventory_Sport
{
    public partial class Fkategori : Form
    {
        DataTable dtKategori;
        koneksi db = new koneksi();

        public Fkategori()
        {
            InitializeComponent();
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
 if (this.kategori == null) 
 {
            this.kategori = new model.kategori();
 }
  if (this.kategori.KategoriID == 0)
  {
   this.db.execQueryProc("addKategori", new object[] 
   {
    this.kategori.Kode,
    this.kategori.NmKategori,
    this.kategori.Deskripsi
   });
  }
        }
    }//end class
}
sekarang bagaimana contoh store proc yang menggunakan parameter silahkan baca di contoh add data dengan proc melempar parameter

Good luck !!!

No comments:

Post a Comment