我的这个函数给出错误执行读者需要打开只读连接你的连接状态是连接..... [英] my this function gives error execute reader require open read only connection your state of connection is connecting.....

查看:79
本文介绍了我的这个函数给出错误执行读者需要打开只读连接你的连接状态是连接.....的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  private   void 注意()
{
string Textlength = string .Empty;

DataTable dt2 = new DataTable();

SqlDataAdapter da2 = new SqlDataAdapter();


尝试
{

if (con1.State == ConnectionState.Closed)
{
con1.Open();
}

da2 = new SqlDataAdapter( 从tblKhadiGramUpdates中选择*其中IsAlive ='True'按AutoId desc排序,con1);

da2.Fill(dt2);

if (dt2.Rows.Count& gt; 0
{
int count = Convert.ToInt32(dt2.Rows.Count);

for int i = 0 ; i& lt; count; i ++)
{
Label MyLabel = new Label();

Image Img1 = new Image();

Img1.ImageUrl = 〜/ images / New37.gif;

Img1.ID = Img1;

// 设置标签的文本和ID属性。
MyLabel.Text = dt2.Rows [i] [ 新闻]。ToString();

MyLabel.ID = MyLabel;

MyLabel.Font.Size = 10 ;

PlaceHolder1.Controls.Add(MyLabel);

PlaceHolder1.Controls.Add(Img1);

// 以HTML格式添加间隔< br />元素。
PlaceHolder1.Controls.Add( new LiteralControl( < br />));

PlaceHolder1.Controls.Add( new LiteralControl( < br />));

}
}
}
catch (例外情况)
{
var message = new JavaScriptSerializer()。Serialize(ex.Message.ToString());
var script = string .Format( alert({0});,message);
ScriptManager.RegisterClientScriptBlock(Page,Page.GetType(), Alert,脚本, true );
return ;

}
最后
{
con1.Close();

da2.Dispose();

dt2.Clear();

dt2.Dispose();
}
}

解决方案

根据您的代码,您似乎将连接存储在方法之外。你似乎也关闭它但是根本不处理它。



而不是试图自己管理连接的状态,使用使用 [ ^ ]声明



因此代码应该类似于

< pre lang =c#> 使用(SqlConnection con1 = new SqlConnection(connectionstring)){
da2 = new SqlDataAdapter( select * from tblKhadiGramUpdates Where IsAlive ='True'按AutoId desc排序,con1);
...



如需更多讨论,请查看正确执行数据库操作 [ ^ ]


private void Notice()
    {
        string Textlength = string.Empty;

        DataTable dt2 = new DataTable();

        SqlDataAdapter da2 = new SqlDataAdapter();


        try
        {

            if (con1.State == ConnectionState.Closed)
            {
                con1.Open();
            }

            da2 = new SqlDataAdapter("Select * from tblKhadiGramUpdates Where IsAlive='True' Order By AutoId desc", con1);

            da2.Fill(dt2);

            if (dt2.Rows.Count &gt; 0)
            {
                int count = Convert.ToInt32(dt2.Rows.Count);

                for (int i = 0; i &lt; count; i++)
                {
                    Label MyLabel = new Label();

                    Image Img1 = new Image();

                    Img1.ImageUrl = "~/images/New37.gif";

                    Img1.ID = "Img1";

                    // Set the label's Text and ID properties.
                    MyLabel.Text = dt2.Rows[i]["News"].ToString();

                    MyLabel.ID = "MyLabel";

                    MyLabel.Font.Size = 10;

                    PlaceHolder1.Controls.Add(MyLabel);

                    PlaceHolder1.Controls.Add(Img1);

                    // Add a spacer in the form of an HTML <br /> element.
                    PlaceHolder1.Controls.Add(new LiteralControl("<br />"));

                    PlaceHolder1.Controls.Add(new LiteralControl("<br />"));

                }
            }
        }
        catch (Exception ex)
        {
            var message = new JavaScriptSerializer().Serialize(ex.Message.ToString());
            var script = string.Format("alert({0});", message);
            ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "Alert", script, true);
            return;

        }
        finally
        {
            con1.Close();

            da2.Dispose();

            dt2.Clear();

            dt2.Dispose();
        }
    }

解决方案

Based on your code, it looks like you store the connection outside the method. Also you seem to close it but not dispose it at all.

Instead of trying to manage the connection's state by yourself, use the using[^] statement

So the code should look something like

using (SqlConnection con1 = new SqlConnection(connectionstring))  {
   da2 = new SqlDataAdapter("Select * from tblKhadiGramUpdates Where IsAlive='True' Order By AutoId desc", con1);
...


For more discussion, have a look at Properly executing database operations[^]


这篇关于我的这个函数给出错误执行读者需要打开只读连接你的连接状态是连接.....的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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