[英]DataReader Associated with this Command Error
OleDbConnection baglanti =
new OleDbConnection("connect");
OleDbCommand komut = new OleDbCommand();
OleDbDataReader dr;
DataSet ds = new DataSet();
OleDbDataAdapter da = new OleDbDataAdapter();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if(textBox1.Text=="" || textBox2.Text=="" || textBox3.Text=="" || maskedTextBox1.Text=="" || dateTimePicker1.Text=="" || maskedTextBox2.Text=="" || textBox6.Text=="")
{
MessageBox.Show("Lütfen Boş alanları doldurunuz !!");
}
else
{
baglanti.Open();
komut.Connection = baglanti;
komut.CommandText=("Select TcKimlik from Hasta");
dr = komut.ExecuteReader();
while(dr.Read())
{
if(dr["TcKimlik"].ToString()==textBox1.Text.ToString())
{
MessageBox.Show("aaaaa");
}
else
{
komut.CommandText = ("insert into Hasta (TcKimlik,h_adı,h_soyadı,tel_no,ran_tar,ran_saati,sikayet_nedeni) values('" + textBox1.Text.ToString() + "','" + textBox2.Text.ToString() + "','" + textBox3.Text.ToString() + "','" + maskedTextBox1.Text.ToString() + "','" + dateTimePicker1.Text.ToString() + "','" + maskedTextBox2.Text.ToString() + "','" + textBox6.Text.ToString() + "')");
komut.ExecuteNonQuery();
MessageBox.Show("Kayıt Başarı İle Tamamlandı", "Kayıt Başarılı", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
baglanti.Close();
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
maskedTextBox1.Clear();
maskedTextBox2.Clear();
textBox6.Clear();
}
}
I have tried a lot of ways but I could not go to a solution. 我尝试了很多方法,但是无法解决。 If I check the data or make
dr.Close()
before registering the data, I can save the data, but then I get an error because I close the data reader prematurely. 如果在注册数据之前检查数据或设置
dr.Close()
可以保存数据,但是由于我过早关闭数据读取器而收到错误消息。 I want to check the data in the database but I get an error like below. 我想检查数据库中的数据,但出现如下错误。
Associated with this Command, there is already an explicit DataReader that should be closed first. 与此命令相关联,已经有一个明确的DataReader应该首先关闭。
Issue with you code is you are making use of Command
object for performing reading and inserting
record at one time. 与您的代码有关的问题是您正在使用
Command
对象一次执行reading and inserting
记录。
you require to perform operation one by one , you cannot do both in one time. 您需要一个接一个地执行操作,则不能一次执行两个操作。 so you code should be like this
所以你的代码应该像这样
using (OleDbconnection connection = new OleDbconnection (ConnectionString))
{
connection.Open();
OleDbCommand select = new OleDbCommand("..selecte query",connection);
OleDbDataReader reader = select.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
//read data you want
//you might not need another connection , but i added to seperate it , you can reuse connection you created and perfrom update
using(OleDbconnection connection1 = new OleDbconnection (ConnectionString))
{
OleDbCommand insert= new OleDbCommand ("..insertquery", connection1);
insert.ExecuteNonQuery();
}
}
}
reader.Close();
}
}
you can use command object to perfrom multiple task but it should be one by one , example 您可以使用命令对象来执行多个任务,但是它应该一个接一个的示例
//first complete reading and close reader
//second do insert operation complete it
//third do update operation
One more suggstion make use of Parameter
as your insert query is open for sql injection attack, if you go with parameterise query can resolve that issue 还有一个suggstion使用的
Parameter
为你的插入查询是开放的SQL注入攻击,如果用参数化查询可以去解决这个问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.