连接的当前状态是打开的。在c#win app中 [英] The connection's current state is open. in c# win app
问题描述
public bool existPationts()
{
con.Open();
s =select * from Patients where P_id =+
Convert.ToString(txt_Pid.Text);
sCommand = new SqlCommand(s,con);
int isexist;
isexist = Convert.ToInt32(sCommand.ExecuteScalar());
if(isexist> 0)
{
MessageBox .Show(它存在);
txt_Pid.Focus();
txt_Pid.Clear();
返回true;
con.Close();
}
else
{
返回false;
con.Close();
}
private void btn_Save_Click (对象发送者,EventArgs e)
{
试试
{
if(!existPationts())
{
验证();
if(validateID == true&& validateName == true&& validatePhone1
== true&& validateRadio == true&& validateCB_City == true
&& validateCB_Center == true&& validateCB_doctor ==
true&& validateCB_Side == true)
{
if(MessageBox.Show(你要保存吗?,保存,MessageBoxButtons。 YesNo,MessageBoxIcon.Question)== DialogResult.Yes)
{
s =插入患者(P_id,P_Name,P_Gender,
P_DateOfBirth,City_id,Center_id,Phone1,Phone2,
Doctor_id,Side_id,P_Weight,P_Length,Notes);
s = s +值(@P_id,@ P_Name,@ P_Gender,
@P_DateOfBirth,@ City_id,@ Center_id,@ Phone1,@ Phone2,
@Doctor_id,@ Side_id,@ P_Weight, @P_Length,@注意);
sCommand =新的SqlCommand(s,con);
con.Open();
sCommand.Parameters.AddWithValue(@ P_id,txt_Pid.Text);
sCommand.Parameters.AddWithValue(@ P_Name,
txt_PName.Text);
sCommand.Parameters.AddWithValue(@ P_Gender,x);
sCommand.Parameters.AddWithValue(@ P_DateOfBirth,
dtp_PBirthDate.Text ); $ / b $ $ $ $ s $ s $ $ $ $ $ $ $ $ $ $ $ Center_id,
CB_Center.SelectedValue);
sCommand.Parameters.AddWithValue(@ Phone1,
txt_Phone1.Text);
sCommand.Parameters.AddWithValue(@ Phone2,
txt_Phone2.Text);
sCommand.Parameters.AddWithValue(@ Doctor_id,
CB_Doctor.SelectedValue);
sCommand.Parameters.AddWithValue (@ Side_id,
CB_Side.SelectedValue);
sCommand.Parameters.AddWithValue(@ P_Weight,
txt_Weight.Text) ; $ / b $ s $ b $ com $。 ,
txt_Notes.Text);
sCommand.ExecuteNonQuery();
con.Close();
MessageBox.Show(它已保存,MessageBoxButtons.OK,MessageBoxIco n。信息);
}
其他
{
MessageBox.Show(取消); < br $>
}
}
}
}
catch
{
return;
}
}
我尝试了什么:
如何在此代码中解决此问题
public bool existPationts()
{
con.Open();
s = "select * from Patients where P_id = " +
Convert.ToString(txt_Pid.Text);
sCommand = new SqlCommand(s, con);
int isexist;
isexist = Convert.ToInt32(sCommand.ExecuteScalar());
if(isexist > 0)
{
MessageBox.Show("its exist");
txt_Pid.Focus();
txt_Pid.Clear();
return true;
con.Close();
}
else
{
return false;
con.Close();
}
private void btn_Save_Click(object sender, EventArgs e)
{
try
{
if(!existPationts())
{
Validation();
if (validateID == true && validateName == true && validatePhone1
== true && validateRadio == true && validateCB_City == true
&& validateCB_Center == true && validateCB_doctor ==
true && validateCB_Side == true)
{
if (MessageBox.Show("Are you want save?", "save", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
s = "insert into Patients (P_id, P_Name, P_Gender,
P_DateOfBirth, City_id, Center_id, Phone1, Phone2,
Doctor_id, Side_id, P_Weight, P_Length, Notes)";
s = s + "values (@P_id, @P_Name, @P_Gender,
@P_DateOfBirth, @City_id, @Center_id, @Phone1, @Phone2,
@Doctor_id, @Side_id, @P_Weight, @P_Length, @Notes)";
sCommand = new SqlCommand(s, con);
con.Open();
sCommand.Parameters.AddWithValue("@P_id", txt_Pid.Text);
sCommand.Parameters.AddWithValue("@P_Name",
txt_PName.Text);
sCommand.Parameters.AddWithValue("@P_Gender", x);
sCommand.Parameters.AddWithValue("@P_DateOfBirth",
dtp_PBirthDate.Text);
sCommand.Parameters.AddWithValue("@City_id",
CB_City.SelectedValue);
sCommand.Parameters.AddWithValue("@Center_id",
CB_Center.SelectedValue);
sCommand.Parameters.AddWithValue("@Phone1",
txt_Phone1.Text);
sCommand.Parameters.AddWithValue("@Phone2",
txt_Phone2.Text);
sCommand.Parameters.AddWithValue("@Doctor_id",
CB_Doctor.SelectedValue);
sCommand.Parameters.AddWithValue("@Side_id",
CB_Side.SelectedValue);
sCommand.Parameters.AddWithValue("@P_Weight",
txt_Weight.Text);
sCommand.Parameters.AddWithValue("@P_Length",
txt_Length.Text);
sCommand.Parameters.AddWithValue("@Notes",
txt_Notes.Text);
sCommand.ExecuteNonQuery();
con.Close();
MessageBox.Show("it's saved", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("Cancel");
}
}
}
}
catch
{
return;
}
}
What I have tried:
How i solve this problem in this code
推荐答案
你的连接当前状态是开放的。
当你打开连接时,最好在下面做类型
Your Connection Current State is Open.
When you open connection, best practice to do below type
if(con.State == con.Open)
{
lable1.text= "Connection is Open";
}
else
{
lable1.text= "Connection is close";
}
在您的密码中。打开连接时应用条件
In your Code. apply a condition when you open your connection
if(con.State != con.Open)
{
con.Open();
}
问候,
AARIF SHAIKH
Regards,
AARIF SHAIKH
试试这个
public bool existPationts()
{
con .Open();
s =选择来自患者,其中P_id =+
Convert.ToString(txt_Pid.Text);
sCommand = new SqlCommand(s,con);
int isexist;
isexist = Convert.ToInt32(sCommand.ExecuteScalar());
if (isexist> 0)
{
MessageBox.Show(它存在);
txt_Pid.Focus();
txt_Pid.Clear();
con.Close();
返回true;
}
其他
{
con.Close();
返回false;
}
Try this
public bool existPationts()
{
con.Open();
s = "select * from Patients where P_id = " +
Convert.ToString(txt_Pid.Text);
sCommand = new SqlCommand(s, con);
int isexist;
isexist = Convert.ToInt32(sCommand.ExecuteScalar());
if(isexist > 0)
{
MessageBox.Show("its exist");
txt_Pid.Focus();
txt_Pid.Clear();
con.Close();
return true;
}
else
{
con.Close();
return false;
}
这篇关于连接的当前状态是打开的。在c#win app中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!