0

这是我的aspx.cs页面,这些代码应该将值插入到数据库中,但目前它并不好,它没有插入到数据库中,我没有收到任何错误但插入没有发生,我不知道出了什么问题。IS有可能SqlException吗?或任何其他问题?

   protected void btnSkipSubmit_Click(object sender, EventArgs e)
{
    int random = 0;
    bool isValidInt = int.TryParse(txtrandom.Text, out random);
    //string dummmy = "D";
    //int dum = 0;
    Patient p = new Patient();

    //PatientBill pb = new PatientBill();
    myConnection obj1 = new myConnection();
    DateTime sdt = DateTime.Now;
   // string a;
   string str = ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;
   string cmdString = "";
   SqlConnection con = new SqlConnection(str);
   SqlCommand cmd = new SqlCommand(cmdString, con);
   SqlTransaction transaction;

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

            //a = obj1.fnSkipPatient(p);

            string dummmy = "D";
            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,@CurrDept,@NextDept,@PayID,@PayDet1,@PayDet2,@PayDet3,@PayValidity,@Archived,@UpdateUser,@UpdateShift,@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 = p.HospitalNo;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


                cmd.CommandType = CommandType.Text;

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

            }
            catch (Exception ex)
            {

            }
            finally
            {
                con.Close();
            }
        }
    }

    }
4

4 回答 4

1

移动cmd.Parameters.Clear();cmd.ExecuteNonQuery()

仅尝试cmd.ExecuteNonQuery()

另请注意,如果您的isValidIntis false,则不会执行您的查询。因此,首先检查您的是否isValidInt为 true。最好改变你的逻辑

于 2013-10-24T06:54:31.790 回答
0

在服务器管理工​​作室中执行以下代码......

GRANT INSERT ON [dbo].[Patient_Data] TO public

希望它有效...

于 2013-10-24T06:55:05.053 回答
0

您必须在事务中提交更改并transaction.Rollback();catch

我想你之后失踪transaction.Commit();cmd.ExecuteNonQuery();

        try
        {
            con.Open();
            transaction = con.BeginTransaction();
            cmd = new SqlCommand(cmdString, con, transaction);

            cmd.Parameters.AddWithValue("@PatientID", p.HospitalNo);
            // Continue your usual work
            cmd.Parameters.AddWithValue("@Remark", dummmy);

            cmd.CommandType = CommandType.Text;
            cmd.ExecuteNonQuery();
            transaction.Commit();
            cmd.Parameters.Clear();
        }
        catch (Exception ex)
        {
            transaction.Rollback();
        }
        finally
        {
            con.Close();
        }

类似于上面代码的东西应该适合你。

希望能帮助到你。

于 2013-10-24T06:55:14.713 回答
0

1)确保连接字符串有效。

2)如果关闭并打开sql server management studio,数据库将切换到“master”,您必须将控件更改为您的DB。您可以在sql编辑器工具栏中看到一个下拉框,您可以在其中选择DB应该执行当前查询的位置。

于 2013-10-24T09:50:24.387 回答