ExecuteReader:连接属性已被删除

我的编码是

protected void Button2_Click(object sender, EventArgs e)
    {

       SqlConnection conn = new SqlConnection("Data Source=Si-6\\SQLSERVER2005;Initial Catalog=rags;Integrated Security=SSPI");

    SqlDataReader rdr = null;

    try
    {
        // 2. Open the connection
        conn.Open();

        // 3. Pass the connection to a command object
        //SqlCommand cmd = new SqlCommand("select * from Customers", conn);
        SqlCommand cmd=new SqlCommand ("insert into time(project,iteration)
                  values('"+this .name1 .SelectedValue +"','"+this .iteration .SelectedValue +"')");

        //
        // 4. Use the connection
        //

        // get query results
        rdr = cmd.ExecuteReader();

        // print the CustomerID of each record
        while (rdr.Read())
        {
            Console.WriteLine(rdr[0]);
        }
    }
    finally
    {
        // close the reader
        if (rdr != null)
        {
            rdr.Close();
        }

        // 5. Close the connection
        if (conn != null)
        {
            conn.Close();
        }
    }
  }
  }

    }

推荐答案

使用此和传递连接对象:

 SqlCommand cmd=new SqlCommand ("insert into time(project,iteration)values('"+this .name1 .SelectedValue +"','"+this .iteration .SelectedValue +"')",conn);

Asp.net相关问答推荐

如何从 Azure 上托管的应用服务获取登录用户名?

如何在 C#/MVC 4 中的 Html.TextBoxFor 中输入占位符文本

使用 Asp.Net MVC 和 KnockoutJS 处理日期

Firefox 和 Chrome 之间 1 像素的行高差

无法连接到 ASP.Net 开发服务器问题

解析器错误消息:文件/TestSite/Default.aspx.cs不存在

在 ASP.NET 中更改错误消息的语言

在程序集中找不到上下文类型

如何在没有实体框架的情况下使用 ASP.NET Identity 3.0

倒带请求正文流

使用 gridview asp.net 进行排序和分页

什么是实体框架中的复杂类型以及何时使用它?

缩小 ASP.NET 应用程序的 Html 输出

在 ASP.NET 中实现 404 的最佳方法

在 JavaScript 中获取当前会话值?

在 ASP.NET 中使用 MasterPages 时使用 JQuery 的正确方法?

投票有什么问题?

ASP.NET 5、EF 7 和 SQLite - SQLite 错误 1:没有这样的表:博客

C# - 将图像输出到响应输出流给出 GDI+ 错误

在 ASP.NET 中获取服务器的 IP 地址?