在c#上输入新的数据不会被添加到数据库SQL Server 2008中

本文关键字:数据库 添加 SQL Server 2008 输入 数据 | 更新日期: 2023-09-27 18:15:14

我在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#中添加数据时,没有错误。但是这些数据不会被添加到我的数据库中。有人能帮帮我吗?抱歉,代码太长了

在c#上输入新的数据不会被添加到数据库SQL Server 2008中

从您的代码中可以看出,您缺少

com.ExecuteNonQuery();