这是我的aspx.cs
页面,这些代码应该将值插入到数据库中,但是目前它不会很好,它没有插入数据库,我没有收到任何错误但插入没有发生,我有不知道出了什么问题。有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();
}
}
}
}
答案 0 :(得分:1)
在cmd.Parameters.Clear();
cmd.ExecuteNonQuery()
仅使用cmd.ExecuteNonQuery()
。
另请注意,如果isValidInt
为false
,则不会执行您的查询。因此,请先检查您的isValidInt
是否属实。更好地改变你的逻辑
答案 1 :(得分:0)
在服务器管理工作室执行以下代码....
GRANT INSERT ON [dbo].[Patient_Data] TO public
希望它有效......
答案 2 :(得分: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();
}
与上述代码类似的东西应该适合你。
希望它有所帮助。
答案 3 :(得分:0)
1)确保连接字符串有效。
2)如果你关闭并打开sql server管理工作室,数据库将切换到“master”,你必须将控件更改为你的数据库。你可以在sql编辑器工具栏中看到一个下拉框,你可以在那里选择当前查询应该执行的数据库。