0

这是我的 C# 代码。

   protected void btnSkipSubmit_Click(object sender, EventArgs e)
  {
    myConnection newobj = new myConnection();
    IdGen newid = new IdGen();

    Patient p = new Patient();
    p.PatientName = "D"; //assigning values of the controls to object data members
    p.F_H_GName = "D";
    p.F_H_G = "D";
    p.AgeOnRegn = 1;
    p.Email = "D";
    p.ContactNo = "D";
    p.Gender = "D";
    p.Married = "D";
    p.AddrLine1 = "D";
    p.AddrLine2 = "D";
    p.AddrLine3 = "D";
    p.City = "D";

    p.PinCode = 1;
    p.Religion = "D";
    p.Occupation = "D";

    p.RegTime = DateTime.Now;

    p.VisitDate = DateTime.Now;
    p.CurrDept = 14;
    p.NextDept = 14;  //if next dept is selected as MRD then save it as Dummy dept

    p.PayId = "FR";
    p.Archived = "n"; //need to be corrected at later stage
    p.UpdateUser = Session["User"].ToString();
    p.UpdateShift = 1;
    p.UpdateDate = DateTime.Parse(DateTime.Now.ToString());

    p.LocId = Session["Location"].ToString();
    p.Remark = "D";
    p.Religion = "D";
    p.PayDet1 = "D";
    p.PayDet2 = "D";
    p.PayDet3 = "D";
    p.PayValidity = DateTime.Now;


    int random = 0;
    bool isValidInt = int.TryParse(txtrandom.Text, out random);
    for (int i = 0; i < random; i++)
    {
    DateTime date = DateTime.Parse(DateTime.Now.ToShortDateString());
            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();


            newid = newobj.fnPatientIdPicker("PatientID", locn);
            int seed1 = newid.Seed;
            int mon1 = newid.Month;
            if (!(mon1.ToString().Equals(month)))
            {
                seed1 = 1;
                mon1 = Convert.ToInt32(month);
            }
            string paddedId = seed1.ToString();
            if (seed1 < 10)
                paddedId = "000" + seed1;
            else if (seed1 < 100)
                paddedId = "00" + seed1;
            else if (seed1 < 1000)
                paddedId = "0" + seed1;
            else
                paddedId = seed1.ToString();

            string patientid = locn + paddedmonth + year + paddedId;
            int retvalue = newobj.fnPatientIdIncrementer(seed1 + 1, mon1, "PatientID", locn);
            p.HospitalNo = patientid;

              string str;
              string cmdString;
              SqlConnection con;
              SqlCommand cmd;
              SqlTransaction transaction;
              str = ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;

              con = new SqlConnection(str);

            cmdString = "INSERT INTO Patient_Data(PatientID,PatientName,F_H_GName,AgeOnRegn,Gender,Married,AddressLine1,City,RegTime,VisitDate,CurrDept,NextDept,PayID,Archived,UpdateUser,UpdateDate,LocID,F_H_G) VALUES (@PatientID,@PatientName,@F_H_GName,@AgeOnRegn,@Gender,@Married,@AddrLine1,@City,@RegTime,@VisitDate,@CurrDept,@NextDept,@PayID,@Archived,@UpdateUser,@UpdateDate,@LocId,@F_H_G)";


            con = new SqlConnection(str);
             try
               {



                  con.Open();
                  transaction = con.BeginTransaction();

                  cmd = new SqlCommand(cmdString, con, transaction);

        cmd.Parameters.Clear();

        cmd.Parameters.Add("@PatientID", SqlDbType.VarChar, 12);
        cmd.Parameters["@PatientID"].Value = p.HospitalNo;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        cmd.Parameters.Add("@Remark", SqlDbType.VarChar, 100);
        cmd.Parameters["@Remark"].Value = p.Remark;
        cmd.CommandType = CommandType.Text;

        cmd.ExecuteNonQuery();
        log.Info("Patient added " + p.HospitalNo);
    }
    catch (Exception ex)
    {
        log.Debug("Error: Inside catch block of fnskippatient");
        log.Error("Error msg:" + ex);
        log.Error("Stack trace:" + ex.StackTrace);


    }
    finally
    {
        con.Close();
    }



    }

}

代码中涉及的函数如下所示

  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();            
    }
}

日志文件消息如下所示

2013-11-07 10:47:17,734 [16] DEBUG myConnection [(null)] - Inside 'fnLogin' -function to      log into the system, returns role if user exists
2013-11-07 10:47:17,750 [16] DEBUG myConnection [(null)] - Inside 'fnGetDepartment' -function to getting department 1
2013-11-07 10:47:17,750 [16] DEBUG myConnection [(null)] - inside 'getdeptcode' method -to get deptcode from deptid= MRD
  2013-11-07 10:47:24,281 [12] DEBUG myConnection [(null)] - gets the max seed value,month value from IdGenerator table for a particular location, used for Hospital number generationIdGen
 2013-11-07 10:47:26,734 [15] DEBUG myConnection [(null)] - gets the max seed value,month value from IdGenerator table for a particular location, used for Hospital number generationIdGen
2013-11-07 10:47:26,734 [15] DEBUG myConnection [(null)] - gets the max seed value,month value from IdGenerator table for a particular location, used for Hospital number generationIdGen
  2013-11-07 10:47:26,734 [15] DEBUG myConnection [(null)] - Inside 'fnPatientIdIncrementer' -Increments the Patient ID seed value for a particular location by 1AT
  2013-11-07 10:47:26,734 [15] INFO  myConnection [(null)] - Info: patient id incremented successfully0
  2013-11-07 10:47:26,734 [15] INFO  ClerkReception_SkipRegaspx [(null)] - Patient added AT1120130046
   2013-11-07 10:47:26,734 [15] DEBUG myConnection [(null)] - gets the max seed value,month value from IdGenerator table for a particular location, used for Hospital number generationIdGen
    2013-11-07 10:47:26,734 [15] DEBUG myConnection [(null)] - Inside 'fnPatientIdIncrementer' -Increments the Patient ID seed value for a particular location by 1AT
    2013-11-07 10:47:26,734 [15] INFO  myConnection [(null)] - Info: patient id incremented successfully0
    2013-11-07 10:47:26,734 [15] INFO  ClerkReception_SkipRegaspx [(null)] - Patient added AT1120130047

我尝试过的代码显示no error no exception但仍然没有将数据插入到数据库中,任何建议表示赞赏。

4

1 回答 1

0

看起来您正在使用事务进行插入,但从不提交。

该文档指出:

您必须使用 Commit 或 Rollback 方法显式提交或回滚事务。

transaction.Commit();之后添加cmd.ExecuteNonQuery();

如果出现异常,您可能还想添加回滚。

这个文档有一个很好的例子。

于 2013-11-07T05:40:06.443 回答