0

我在 C# 上有这段代码

SqlParameter NoIDStaffNew, IDCabangNew, NamaStaffNew, NoTelpStaffNew, JKStaffNew, AlamatStaffNew, GajiStaffNew; 

            NoIDStaffNew = new SqlParameter();
            IDCabangNew = new SqlParameter();
            NamaStaffNew = new SqlParameter();
            NoTelpStaffNew = new SqlParameter();
            JKStaffNew = new SqlParameter ();
            AlamatStaffNew = new SqlParameter();
            GajiStaffNew = new SqlParameter();

            string IDCabangX="";

            SqlCommand com;

            SqlConnection con = new SqlConnection(strCon);

            com = new SqlCommand();
            com.Connection = con;
            con.Open();

            com.CommandType = CommandType.StoredProcedure;
            com.CommandText = "addstaff";


            NoIDStaffNew.SqlDbType = SqlDbType.VarChar;
            NoIDStaffNew.Size = 5;
            NoIDStaffNew.ParameterName = "@NoIDStaff";
            NoIDStaffNew.Value = TextBox_Add_ResidentID.Text.ToString();
            NoIDStaffNew.Direction = ParameterDirection.Input;

            IDCabangNew.SqlDbType = SqlDbType.VarChar;
            IDCabangNew.Size = 5;
            IDCabangNew.ParameterName = "@IDCabang";

            if(comboBox1.SelectedIndex==0){
                IDCabangX = "KG";
            }

            else if(comboBox1.SelectedIndex==1){
                IDCabangX = "TD";
            }

            else if (comboBox1.SelectedIndex == 2) {
                IDCabangX = "PI";

            }

            IDCabangNew.Value = IDCabangX;
            IDCabangNew.Direction = ParameterDirection.Input;



            NamaStaffNew.SqlDbType = SqlDbType.VarChar;
            NamaStaffNew.Size = 20;
            NamaStaffNew.ParameterName = "@NamaStaff";
            NamaStaffNew.Value = TextBox_Add_Name.Text.ToString();
            NamaStaffNew.Direction = ParameterDirection.Input;



            NoTelpStaffNew.SqlDbType = SqlDbType.VarChar;
            NoTelpStaffNew.Size = 15;
            NoTelpStaffNew.ParameterName = "@NoTelpStaff";
            NoTelpStaffNew.Value = TextBox_Add_TelephoneNumber.Text.ToString();
            NoTelpStaffNew.Direction = ParameterDirection.Input;

            string jkx = "";

            JKStaffNew.SqlDbType = SqlDbType.VarChar;
            JKStaffNew.Size = 5;
            JKStaffNew.ParameterName = "@JKStaff";

            if (RadioButton_Add_Male.Checked == true) {
                jkx = "P";
            }
            else if (RadioButton_Add_Female.Checked == true) {
                jkx = "W";
            }
            JKStaffNew.Value = jkx;
            JKStaffNew.Direction = ParameterDirection.Input;



            AlamatStaffNew.SqlDbType = SqlDbType.VarChar;
            AlamatStaffNew.Size = 500;
            AlamatStaffNew.ParameterName = "@AlamatStaff";
            AlamatStaffNew.Value = TextBox_Add_Address.Text.ToString();
            AlamatStaffNew.Direction = ParameterDirection.Input;


            GajiStaffNew.SqlDbType = SqlDbType.Int;
            GajiStaffNew.ParameterName = "@GajiStaff";
            GajiStaffNew.Value = Int32.Parse(textBox1.Text);
            GajiStaffNew.Direction = ParameterDirection.Input;

            com.Parameters.Add(NoIDStaffNew);
            com.Parameters.Add(IDCabangNew);
            com.Parameters.Add(NamaStaffNew);
            com.Parameters.Add(NoTelpStaffNew);
            com.Parameters.Add(JKStaffNew);
            com.Parameters.Add(AlamatStaffNew);
            com.Parameters.Add(GajiStaffNew);

            con.Close();

这个存储过程在 sql 上:

CREATE PROCEDURE addStaff
@NoIDStaff VARCHAR(5),
@IDCabang varchar(5),
@NamaStaff VARCHAR(20),
@NoTelpStaff VARCHAR(15),
@JKStaff VARCHAR(5),
@AlamatStaff VARCHAR(500),
@GajiStaff INT
as

insert into MsStaff(NoIDStaff, IDCabang, NamaStaff, NoTelpStaff, JKStaff, AlamatStaff, GajiStaff, Password)
values(@NoIDStaff, @IDCabang, @NamaStaff, @NoTelpStaff, @JKStaff, @AlamatStaff, @GajiStaff, 'asd')

当我尝试从 C# 添加数据时,没有错误。但数据不会添加到我的数据库中。任何人都可以帮助我吗?抱歉,代码太长了。

4

1 回答 1

3

正如我从你的代码中看到的,你失踪了

com.ExecuteNonQuery();

在关闭连接之前。

于 2013-05-25T17:42:34.957 回答