0

这是一种用于将虚拟元素插入数据库的方法,实际上它可以在没有for循环的情况下正常工作,但是当for插入循环时它会出错。所以Must declare a Scalar variable我知道插入数据库没有重复。

   protected void btnSkipSubmit_Click(object sender, EventArgs e)
{
    myConnection obj1 = new myConnection();
    Patient p = new Patient();
    PatientBill pb = new PatientBill();
    //string a;
    IdGen patid = new IdGen();
    DateTime date = DateTime.Parse(DateTime.Now.ToShortDateString());
    int random = 0;
    bool isValidInt = int.TryParse(txtrandom.Text, out random);


    if (isValidInt)
    {
        for (int i = 0; i < random; i++)
        {

            try
            {
                string locn = Session["Location"].ToString();
                string month = date.Month.ToString();
                string paddedMonth = month.ToString();
                if (month.Length == 1)
                {
                    paddedMonth = 0 + month;
                }
                string year = date.Year.ToString();

                patid = obj1.fnPatientIdPicker("PatientID", locn);

                int id = patid.Seed;
                int mon = patid.Month;
                if (!(mon.ToString().Equals(month)))
                {
                    id = 1;
                    mon = Convert.ToInt32(month);
                }
                string paddedId = id.ToString();
                if (id < 10)
                    paddedId = "000" + id;
                else if (id < 100)
                    paddedId = "00" + id;
                else if (id < 1000)
                    paddedId = "0" + id;
                else
                    paddedId = id.ToString();
                string pid = locn + paddedMonth + year + paddedId;




                string Bill = locn;

                p.HospitalNo = pid;



                obj1.fnPatientIdIncrementer(id + 1, mon, "PatientID", locn);

                obj1.fnSkipPatient(p, pb);


            }
            catch (Exception ex)
            {
                log.Debug("Error: Inside catch block of for loop");
                log.Error("Error msg:" + ex);
                log.Error("Stack trace:" + ex.StackTrace);


            }
        }
    }

}

//fnskip耐心

 public string fnSkipPatient(Patient pa,PatientBill pb)
{

     pa.dummmy = "D";
     pa.dum =0;
     pb.dum1 = 0;
     pa.dumm = DateTime.Now;
     pb.dummmy = "D";
     pb.dum = 0;
     pb.dumm = DateTime.Now;

    //int dum = 0;
    DateTime sdt = DateTime.Now;
    cmdString = "INSERT INTO Patient_Data(PatientID,PatientName,F_H_G,F_H_GName,AgeOnRegn,Email,ContactNo,Gender,Married,AddressLine1,AddressLine2,City,PinCode,Religion,Occupation,RegTime,VisitDate,CurrDept,NextDept,PayID,PayDet1,PayDet2,PayDet3,PayValidity,Archived,UpdateUser,UpdateShift,UpdateDate,LocID,AddressLine3,Remark) VALUES (@PatientID,@PatientName,@F_H_G,@F_H_GName,@AgeOnRegn,@Email,@ContactNo,@Gender,@Married,@AddrLine1,@AddrLine2,@City,@PinCode,@Religion,@Occupation,@RegTime,@VisitDate,14,14,@PayID,@PayDet1,@PayDet2,@PayDet3,@PayValidity,@Archived,@UpdateUser,1,@UpdateDate,@LocId,@AddrLine3,@Remark)";
    con = new SqlConnection(str);
    try
    {
                log.Debug("Entering fnAddPatient method-Function to add a new patient into the database,generate registration bill");
        con.Open();
        transaction = con.BeginTransaction();

        cmd = new SqlCommand(cmdString, con, transaction);
        cmd.Parameters.Add("@PatientID", SqlDbType.VarChar, 12);
        cmd.Parameters["@PatientID"].Value = pa.HospitalNo;

        cmd.Parameters.Add("@PatientName", SqlDbType.VarChar, 30);
        cmd.Parameters["@PatientName"].Value = pa.dummmy;

        cmd.Parameters.Add("@F_H_G", SqlDbType.VarChar, 1);
        cmd.Parameters["@F_H_G"].Value = pa.dummmy;

        cmd.Parameters.Add("@F_H_GName", SqlDbType.VarChar, 30);
        cmd.Parameters["@F_H_GName"].Value = pa.dummmy;

        cmd.Parameters.Add("@AgeOnRegn", SqlDbType.Int);
        cmd.Parameters["@AgeOnRegn"].Value = pa.dum;

        cmd.Parameters.Add("@Email", SqlDbType.VarChar, 40);
        cmd.Parameters["@Email"].Value = pa.dummmy;

        cmd.Parameters.Add("@ContactNo", SqlDbType.VarChar, 12);
        cmd.Parameters["@ContactNo"].Value = pa.dummmy;

        cmd.Parameters.Add("@Gender", SqlDbType.VarChar, 1);
        cmd.Parameters["@Gender"].Value = pa.dum;

        cmd.Parameters.Add("@Married", SqlDbType.VarChar, 1);
        cmd.Parameters["@Married"].Value = pa.dummmy;

        cmd.Parameters.Add("@AddrLine1", SqlDbType.VarChar, 100);
        cmd.Parameters["@AddrLine1"].Value = pa.dummmy;

        cmd.Parameters.Add("@AddrLine2", SqlDbType.VarChar, 100);
        cmd.Parameters["@AddrLine2"].Value = pa.dummmy;

        cmd.Parameters.Add("@AddrLine3", SqlDbType.VarChar, 100);
        cmd.Parameters["@AddrLine3"].Value = pa.dummmy;

        cmd.Parameters.Add("@City", SqlDbType.VarChar, 20);
        cmd.Parameters["@City"].Value = pa.dummmy;

        cmd.Parameters.Add("@PinCode", SqlDbType.Int);
        cmd.Parameters["@PinCode"].Value = pa.dum;

        cmd.Parameters.Add("@Religion", SqlDbType.VarChar, 20);
        cmd.Parameters["@Religion"].Value = pa.dummmy;

        cmd.Parameters.Add("@Occupation", SqlDbType.VarChar, 20);
        cmd.Parameters["@Occupation"].Value = pa.dummmy;

        cmd.Parameters.Add("@RegTime", SqlDbType.DateTime);
        cmd.Parameters["@RegTime"].Value = pa.dumm;

        cmd.Parameters.Add("@VisitDate", SqlDbType.DateTime);
        cmd.Parameters["@VisitDate"].Value = pa.dumm;

        cmd.Parameters.Add("@CurrDept", SqlDbType.Int);
        cmd.Parameters["@CurrDept"].Value = 14;

        cmd.Parameters.Add("@NextDept", SqlDbType.Int);
        cmd.Parameters["@NextDept"].Value = 14;

        cmd.Parameters.Add("@PayId", SqlDbType.VarChar, 2);
        cmd.Parameters["@PayId"].Value = "FR";

        cmd.Parameters.Add("@PayDet1", SqlDbType.VarChar, 15);
        cmd.Parameters["@PayDet1"].Value = pa.dummmy;

        cmd.Parameters.Add("@PayDet2", SqlDbType.VarChar, 50);
        cmd.Parameters["@PayDet2"].Value = pa.dummmy;

        cmd.Parameters.Add("@PayDet3", SqlDbType.VarChar, 15);
        cmd.Parameters["@PayDet3"].Value = pa.dummmy;

        cmd.Parameters.Add("@PayValidity", SqlDbType.DateTime);
        cmd.Parameters["@PayValidity"].Value = pa.dumm;

        cmd.Parameters.Add("@Archived", SqlDbType.VarChar, 1);
        cmd.Parameters["@Archived"].Value = pa.dummmy;

        cmd.Parameters.Add("@UpdateUser", SqlDbType.VarChar, 20);
        cmd.Parameters["@UpdateUser"].Value = "recAT";

        cmd.Parameters.Add("@UpdateShift", SqlDbType.Int);
        cmd.Parameters["@UpdateShift"].Value = pa.dum;

        cmd.Parameters.Add("@UpdateDate", SqlDbType.DateTime);
        cmd.Parameters["@UpdateDate"].Value = pa.dumm;

        cmd.Parameters.Add("@PatientCount", SqlDbType.Int);
        cmd.Parameters["@PatientCount"].Value = pa.dum;

        cmd.Parameters.Add("@LocId", SqlDbType.VarChar, 2);
        cmd.Parameters["@LocId"].Value = "AT";



        cmd.Parameters.Add("@Remark", SqlDbType.VarChar, 100);
        cmd.Parameters["@Remark"].Value = pa.dummmy;

        cmd.CommandType = CommandType.Text;
        cmd.Parameters.Clear();

        string result = cmd.ExecuteNonQuery().ToString();


        cmdString = "SELECT [Value] FROM [App_Conf_M] WHERE" + " ([Property] = 'DummyDepartment') ";
        cmd = new SqlCommand(cmdString, con, transaction);
        int d = Convert.ToInt32(cmd.ExecuteScalar());


        cmdString = "INSERT INTO Patient_Record_Location (PatientID,FromDept,ToDept,Accepted,UpdateDate,LocId)VALUES (@PatientID,@FromDept,@ToDept,'Y',@UpdateDate,@LocId)";
        cmd = new SqlCommand(cmdString, con, transaction);
        cmd.Parameters.Add("@PatientID", SqlDbType.VarChar, 12);
        cmd.Parameters["@PatientID"].Value = pa.HospitalNo;
        cmd.Parameters.Add("@FromDept", SqlDbType.Int);
        cmd.Parameters["@FromDept"].Value = d;
        cmd.Parameters.Add("@ToDept", SqlDbType.Int);
        cmd.Parameters["@ToDept"].Value = d;
        cmd.Parameters.Add("@UpdateDate", SqlDbType.DateTime);
        cmd.Parameters["@UpdateDate"].Value = pa.dumm;
        cmd.Parameters.Add("@LocId", SqlDbType.VarChar, 2);
        cmd.Parameters["@LocId"].Value = "AT";
        cmd.CommandType = CommandType.Text;
        cmd.Parameters.Clear();

        cmd.ExecuteNonQuery();


        return result;

    }
    catch (Exception e)
    {
        log.Debug("Error: Inside catch block of fnaddpatient");
        log.Error("Error msg:" + e);
        log.Error("Stack trace:" + e.StackTrace);
        transaction.Rollback();
        return e.ToString();
    }
    finally
    {
        con.Close();
    }
}

//fnpatidpicker 和 fnpatidincreamenter

   public IdGen fnPatientIdPicker(string item, string location)
{
    cmdString = "SELECT [Seed],[Month] FROM [IdGenerator] WHERE (([Item] = @Item) AND ([Location] = @Location))";
    con = new SqlConnection(str);
    con.Open();
    cmd = new SqlCommand(cmdString, con);
    cmd.Parameters.Add("@Item", SqlDbType.VarChar, 30);
    cmd.Parameters["@Item"].Value = item;
    cmd.Parameters.Add("@Location", SqlDbType.VarChar, 30);
    cmd.Parameters["@Location"].Value = location;

    SqlDataReader reader = cmd.ExecuteReader();
    IdGen pid = new IdGen();
    while (reader.Read())
    {
        pid.Seed = (int)reader["Seed"];
        pid.Month = (int)reader["Month"];
    }
    log.Debug("gets the max seed value,month value from IdGenerator table for a particular location, used for Hospital number generation" + pid);
    con.Close();
    return pid;//returns seed, month value as IdGen object
}

/*Increments the Patient ID seed value for a particular location by 1*/
public int fnPatientIdIncrementer(int NextSeed, int Month, string item, string location)
{
    cmdString = "Update IdGenerator set Seed=@Seed,Month=@Month WHERE (([Item] = @Item) AND ([Location] = @Location))";
    con = new SqlConnection(str);
    try
    {

        con.Open();
        cmd = new SqlCommand(cmdString, con);
        cmd.Parameters.Add("@Seed", SqlDbType.Int);
        cmd.Parameters["@Seed"].Value = NextSeed;
        cmd.Parameters.Add("@Month", SqlDbType.Int);
        cmd.Parameters["@Month"].Value = Month;
        cmd.Parameters.Add("@Item", SqlDbType.VarChar, 30);
        cmd.Parameters["@Item"].Value = item;
        cmd.Parameters.Add("@Location", SqlDbType.VarChar, 30);
        cmd.Parameters["@Location"].Value = location;
        int result = Convert.ToInt32(cmd.ExecuteScalar());
        log.Debug("Inside 'fnPatientIdIncrementer' -Increments the Patient ID seed value for a particular location by 1" + location);
        log.Info("Info: patient id incremented successfully" + result);
         return result;
    }
    catch (Exception e)
    {
        log.Debug("Error: Inside catch block of fnPatientIdIncrementer");
        log.Error("Error msg:" + e);
        log.Error("Stack trace:" + e.StackTrace);
        return 0;
    }
    finally
    {
        con.Close();            
    }
}
4

1 回答 1

0

如果您的问题是“为什么我的 for 循环中没有插入任何内容?”,那是因为它永远不会运行。您将 random 设置为 0,然后检查 0 < 0 是否。它永远不会像现在这样运行。

于 2013-10-30T12:24:44.467 回答