0

我在这个表单中有一个表单名称 fMainProfile 我有数据网格并加载到数据库 sql server

这是我的代码

public partial class fMainProfile : Form
    {
        public fMainProfile()
        {

            InitializeComponent();
            //showdata();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            fProfile op = new fProfile();

            op.ShowDialog();
        }

        public void showdata() {

            try
            {
                String db = "server=localhost;database=bms_setup;uid=sa;pwd=root234"; //string untuk mewakili alamat servers
                //SqlConnectionStringBuilder SQLSERVER = new SqlConnectionStringBuilder();
                //SQLSERVER.DataSource = "hicapture";
                //SQLSERVER.InitialCatalog = "test";
                //SQLSERVER.IntegratedSecurity = true;
                SqlConnection myconnection = new SqlConnection(db);//mengaktifkan atau mengimport database
                myconnection.Open(); // membuka koneksi database

                String sqlExecution = "SELECT satuan.id, satuan.mac, satuan.serial_number, identitas.nomor_urut, identitas.nama as nama_satuan, jenis.detail_jenis, tbl_nama_type.nama_type FROM identitas LEFT JOIN satuan ON (identitas.satuan_id = satuan.id) LEFT JOIN jenis ON (identitas.jenis_id = jenis.t_id_jenis) LEFT JOIN tbl_nama_type ON (identitas.nama_type_id = tbl_nama_type.id)";
                SqlCommand cmd = new SqlCommand(sqlExecution, myconnection); //perintah sql berupa query / properti obyek untuk sebuah objek SqlDataAdapter
                GlobalClass.adap = new SqlDataAdapter(cmd); //Jembatan atau perantara antara aplikasi yang kita buat dengan database sql server melalui koneksi
                SqlCommandBuilder sqlCommandBuilder = new SqlCommandBuilder(GlobalClass.adap);
                GlobalClass.dt = new DataTable(); // gambaran (representasi) dari sebuah table didalam memory yang fungsinya untuk menyimpan data sementara
                GlobalClass.adap.Fill(GlobalClass.dt); // membuat DataTable menjadi sama struktur kolom nya 
                dgView.DataSource = GlobalClass.dt; //mengambil datasource dan memasukkan ke datagrid
                myconnection.Close(); // menutup koneksi
            }
            catch (Exception kesalahan) // menangkap eksepsi/kesalahan
            {
                //MessageBox.Show("Salah input ip atau Password Sql".ToString()); // message
                //MessageBox.Show(kesalahan.ToString()); // message
            }
        }

        /*public void PerformRefresh()
        {
            this.dgView.Text = DateTime.Now.ToLongTimeString();
        }*/

        private void fMainProfile_Load(object sender, EventArgs e)
        {
            try
            {

                showdata();

            }

            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());

            }
        }

    }

我创建了一个类名DataTableGlobalClassSqlDataAdapter

这是我的代码

class GlobalClass
    {
        public static SqlDataAdapter adap;

        public static DataTable dt;
    }

方法 button1_Click 是打开表单输入数据,类fProfile。在这个fProfile我有一个按钮来保存所有输入文本到 sql server。

这是我的代码

public void saveBtn_Click(object sender, System.EventArgs e)
        {
             try
            {
                    if (txtMac.Text.Trim() == "" || txtSerial.Text.Trim() == "" || txtNomor.Text.Trim() == "" || txtNama.Text.Trim() == "" || txtSandi.Text.Trim() == "")
                    {
                        MessageBox.Show("Please fill all the entry....");
                    }
                    else
                    {

                        Dictionary<String, String> dataSatuan = new Dictionary<String, String>();
                        Dictionary<String, String> dataIden = new Dictionary<String, String>();
                        Dictionary<String, String> dataSatSqlserv = new Dictionary<String, String>();
                        Dictionary<String, String> dataIndSqlserv = new Dictionary<String, String>();
                        dataSatuan.Add("mac", this.txtMac.Text);
                        dataSatuan.Add("serial_number", this.txtSerial.Text);
                        cDatabaseSQLite.Insert("satuan", dataSatuan);

                        //int satuanIDSqlite = 0;
                        /*String db = "setup.sqlite";
                        String s = String.Format("Data Source={0}", db);
                        SQLiteConnection cnn = new SQLiteConnection(s);
                        //SQLiteCommand cmd1 = cnn.CreateCommand();
                        string sql1 = "SELECT id FROM satuan where mac = '" + this.txtMac.Text + "'";
                        cnn.Open();
                        SQLiteCommand mycommand = new SQLiteCommand(cnn);
                        mycommand.CommandText = sql1;
                        int satuanIDSqlite = ((int)mycommand.ExecuteScalar());
                        cnn.Close();*/
                        String db = "setup.sqlite";
                        String s = String.Format("Data Source={0}", db);
                        SQLiteConnection cnn = new SQLiteConnection(s);
                        String sql1 = "SELECT id FROM satuan where serial_number = " + this.txtSerial.Text + "";
                        cnn.Open();
                        SQLiteCommand mycommand = new SQLiteCommand(cnn);
                        mycommand.CommandText = sql1;
                        object value = mycommand.ExecuteScalar();
                        cnn.Close();

                        dataIden.Add("satuan_id", value.ToString());
                        dataIden.Add("nomor_urut", this.txtNomor.Text);
                        dataIden.Add("nama", this.txtNama.Text);
                        dataIden.Add("sandi", this.txtSandi.Text);
                        dataIden.Add("jenis_id", cmbJenis.SelectedValue.ToString());
                        dataIden.Add("nama_type_id", cmbType.SelectedValue.ToString());
                        //
                        dataSatSqlserv.Add("mac", this.txtMac.Text);
                        dataSatSqlserv.Add("serial_number", this.txtSerial.Text);
                        cDatabaseSQLServer.Insert("satuan", dataSatSqlserv);

                        int satuanID = 0;
                        SqlConnection connection = new SqlConnection("server=localhost;database=bms_setup;uid=sa;pwd=root234");
                        SqlCommand cmd = connection.CreateCommand();
                        cmd.CommandText = "SELECT id FROM satuan where serial_number = " + this.txtSerial.Text + "";
                        connection.Open();
                        satuanID = ((int)cmd.ExecuteScalar());
                        connection.Close();
                        //buat query select 
                        dataIndSqlserv.Add("satuan_id", satuanID.ToString());
                        dataIndSqlserv.Add("nomor_urut", this.txtNomor.Text);
                        dataIndSqlserv.Add("nama", this.txtNama.Text);
                        dataIndSqlserv.Add("sandi", this.txtSandi.Text);
                        dataIndSqlserv.Add("jenis_id", cmbJenis.SelectedValue.ToString());
                        dataIndSqlserv.Add("nama_type_id", cmbType.SelectedValue.ToString());

                        cDatabaseSQLite.Insert("identitas", dataIden);

                        cDatabaseSQLServer.Insert("identitas", dataIndSqlserv);


                        //_owner.PerformRefresh();

                        GlobalClass.adap.Update(GlobalClass.dt);

                        this.Close(); 

                    }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

fMainProfile问题是当我单击保存按钮并关闭表单时如何更新数据网格fProfile

当我尝试表单fProfile成功保存到 sql server 并关闭但表单中的数据网格fMainProfile不会更新或刷新。

如何解决?

4

1 回答 1

2

保存到 sql 后调用显示数据,如下所示

 private void button1_Click(object sender, EventArgs e)
    {
        fProfile op = new fProfile();

        op.ShowDialog();

        showdata();
    }
于 2013-07-02T03:44:53.850 回答