コード例 #1
0
        private void createUser()
        {
            string        errInsert = string.Empty;
            cKoneksi      koneksi   = new cKoneksi();
            SqlConnection conn      = new SqlConnection(koneksi.konekMsSql('1'));//koneksi ke 64 db parkir

            cQuery     qr  = new cQuery();
            SqlCommand cmd = null;

            string aplication = string.Empty;

            if (rbAkademik.Checked == true)
            {
                aplication = "Akademik";
            }
            else if (rbBiroUmum.Checked == true)
            {
                if (rbGate.Checked == true)
                {
                    aplication = "Gate";
                }
                else if (rbKantor.Checked == true)
                {
                    aplication = "Biro Umum";
                }
            }

            try
            {
                conn.Open();

                cmd = new SqlCommand(qr.qInsertUser(), conn);

                cmd.Parameters.AddWithValue("@username", txt_username.Text);
                cmd.Parameters.AddWithValue("@fullname", txt_fullname.Text);
                cmd.Parameters.AddWithValue("@password", txt_password.Text);
                cmd.Parameters.AddWithValue("@role", combo_role.Text);
                cmd.Parameters.AddWithValue("@aplication", aplication);

                cmd.ExecuteNonQuery();//insert ke sql
            }
            catch (Exception ex)
            {
                errInsert = ex.Message;
                MessageBox.Show(errInsert, "BSI UMY", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                conn.Close();
                conn.Dispose();
            }

            if (errInsert == string.Empty)
            {
                label_message.Text = "Registrasi User Berhasil Disimpan";
            }

            //cKoneksi koneksi = new cKoneksi();
            //cQuery qr = new cQuery();
            //using (var conn = new SqlConnection(koneksi.konekMsSql('1')))//koneksi ke 64 db parkir
            //{
            //    try
            //    {

            //        conn.Open();//buka koneksi
            //        using (var cmd = new SqlCommand(conn))
            //        {
            //            using (var transaction = conn.BeginTransaction())
            //            {
            //                cmd.CommandText = qr.qInsertUser(); // " INSERT INTO user (username ,fullname ,password ,role) VALUES (@username,@fullname,@password,@role)";
            //                    cmd.Parameters.Clear();
            //                    cmd.Prepare();
            //                    cmd.Parameters.AddWithValue("@username", txt_username.Text);
            //                    cmd.Parameters.AddWithValue("@fullname", txt_fullname.Text);
            //                    cmd.Parameters.AddWithValue("@password", txt_password.Text);
            //                    cmd.Parameters.AddWithValue("@role", combo_role.Text);
            //                    cmd.ExecuteNonQuery();

            //                transaction.Commit();
            //            }
            //        }
            //    }
            //    catch
            //    {
            //        MessageBox.Show("Username sudah dipakai, coba yang lain");
            //    }
            //    finally
            //    {
            //        conn.Close(); //tutup koneksi
            //        conn.Dispose();
            //    }

            //}
        }