已经有一个openDataReader [英] There is already an openDataReader

查看:69
本文介绍了已经有一个openDataReader的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,



im使用此代码。





private void btnSave_Click(object sender,EventArgs e)

{

try

{

string connstr = @ Server =。\ SQLEXPRESS; Initial Catalog = RPSJDB; Integrated Security = True; Max Pool Size = 100;

string query =insert into CustomerTable values('txtCustomerName.Text +' ,'+ txtAddress.Text +','

+ txtMobileNo.Text +','+ lblGoldBalance.Text +','+ lblSilverBalance.Text +',' + lblCashBalance.Text +');

SqlConnection conn = new SqlConnection(connstr);



if(txtCustomerName.Text! =&txtAddress.Text!=)

{

MessageBox.Show(输入一些输入);

conn。打开();

//检查CustomerTable中是否存在用户名



string query1 =从CustomerTable中选择txtCustomerName,其中txtCustomerName ='+ txtCustomerName.Text +';

SqlCommand cmd = new SqlCommand(query1,conn);

SqlDataReader reader = cmd.ExecuteReader();



if(reader!= null && reader.HasRows)

{

// db中存在用户做某事

MessageBox.Show(用户已经存在!!);

}



其他

{

SqlCommand cmd1 = new SqlCommand(query,conn);

cmd1.ExecuteNonQuery();

txtCustomerName.Clear();

txtAddress.Clear();

txtMobileNo.Clear();

MessageBox.Show(值保存在数据库中);

}

reader.Close();

conn.Close();

}

else

{

MessageBox。显示(只有移动字段可以为空);

}

}

catch(exception ex)

{

MessageBox。显示(至少0值可以保存在金银现金区,ex.Message);

}







当我只填写客户名值然后保存时显示错误(这里显示错误只有移动字段可以为空但它不显示。

它显示一个新错误:已经有一个与此命令关联的openDataReader必须先关闭。我已经关闭了读者。

Dear All,

i m using this code.


private void btnSave_Click(object sender, EventArgs e)
{
try
{
string connstr = @"Server=.\SQLEXPRESS ;Initial Catalog=RPSJDB;Integrated Security=True; Max Pool Size=100";
string query = "insert into CustomerTable values('" + txtCustomerName.Text + "','" + txtAddress.Text + "','"
+ txtMobileNo.Text + "','" + lblGoldBalance.Text + "','" + lblSilverBalance.Text + "','" + lblCashBalance.Text + "')";
SqlConnection conn = new SqlConnection(connstr);

if (txtCustomerName.Text != "" & txtAddress.Text != "")
{
MessageBox.Show("Enter some input");
conn.Open();
//Checking User Name Exists in CustomerTable

string query1 = "select txtCustomerName from CustomerTable where txtCustomerName='" + txtCustomerName.Text + "'";
SqlCommand cmd = new SqlCommand(query1,conn);
SqlDataReader reader = cmd.ExecuteReader();

if (reader != null && reader.HasRows)
{
//User exists in db do something
MessageBox.Show("User Already Exists!!");
}

else
{
SqlCommand cmd1 = new SqlCommand(query, conn);
cmd1.ExecuteNonQuery();
txtCustomerName.Clear();
txtAddress.Clear();
txtMobileNo.Clear();
MessageBox.Show("Values Save in DataBase");
}
reader.Close();
conn.Close();
}
else
{
MessageBox.Show("Only Mobile Field Can be Empty");
}
}
catch (Exception ex)
{
MessageBox.Show("At Least 0 value can be save in the Gold Silver Cash Area", ex.Message);
}



it show an error when i am fill only Customer name value then save (here it's show an error "only Mobile field can be empty" but it is not show.
it show a new error is : "There is already an openDataReader associated with this command which must be close first." i am already close the reader.

推荐答案

你应该使用try / catch / finally,而不仅仅是try / catch。如果你遇到错误,那么连接和阅读器永远不会关闭。如果你最后使用,那么不论读者和连接是否有错误将被关闭。您还需要在try / catch / finall之上声明您的阅读器和连接对象y block。就像这样...

You should use try/catch/finally and not just try/catch. If you encounter an error then the connection and reader are never closed. If you use finally then regardless if there is an error or not the reader and connection will be closed. You will also need to declare your reader and connection objects above the try/catch/finally block. Like so...
SqlConnection conn = null; // declare here but set in try block
SqlDataReader reader = null; // declare here but set in try block
try
{
 // ...
}
catch
{
    MessageBox.Show("At Least 0 value can be save in the Gold Silver Cash Area", ex.Message);
}
finally
{
    if(reader != null)
    {
        reader.Close();
    }

    if(conn != null)
    {
        conn.Close();
    }
}



此外,在尝试建立另一个连接之前关闭阅读器。


Also, close the reader before you attempt to make another connection.

if (reader != null && reader.HasRows)
{
//User exists in db do something
MessageBox.Show("User Already Exists!!");
}
else
{
    if(reader != null)
    {
        reader.Close(); // close the reader before making a new connection
    }
    SqlCommand cmd1 = new SqlCommand(query, conn);
    cmd1.ExecuteNonQuery();
    txtCustomerName.Clear();
    txtAddress.Clear();
    txtMobileNo.Clear();
    MessageBox.Show("Values Save in DataBase");
}


这篇关于已经有一个openDataReader的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆