ExecuteReader:连接属性尚未初始化 [英] ExecuteReader: Connection property has not been initialized

查看:41
本文介绍了ExecuteReader:连接属性尚未初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ExecuteReader:连接属性有未初始化.

ExecuteReader: Connection property has not been initialized.

我的编码是

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();
        }
    }
  }
  }

    }

推荐答案

使用这个并传递连接对象:

use this and pass connection object :

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

这篇关于ExecuteReader:连接属性尚未初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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