【问题标题】:ExecuteReader: Connection property has not been initializedExecuteReader:连接属性尚未初始化
【发布时间】:2011-05-03 06:47:23
【问题描述】:

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

    }

【问题讨论】:

  • 由于 SqlConnection、SqlCommand 和 SqlReader 对象正在使用非托管资源,因此它们是一次性对象,因此最好在完成任务时将它们释放掉。为了使代码更具可读性,您可以使用 using 指令来完成。
  • 这些答案是正确的。你必须接受。你必须用你创建的连接初始化 sqlcommand 连接属性。

标签: c# asp.net sql-server-2005


【解决方案1】:

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

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

【讨论】:

  • 嗨,谢谢你的信息。我也有另一个澄清如何在 c# 中获取 html 控件
【解决方案2】:

SqlCommand cmd=new SqlCommand ("insert into time(project,iteration)values('....之后 添加

cmd.Connection = conn;

希望有帮助

【讨论】:

    【解决方案3】:

    你必须为你的命令对象分配连接,比如..

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

    【讨论】:

      【解决方案4】:

      所有答案都是正确的。这是另一种方式。我喜欢这个

      SqlCommand cmd = conn.CreateCommand()
      

      你必须注意到字符串 concat 存在 sql 注入问题。 使用参数 http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.parameters.aspx

      【讨论】:

        【解决方案5】:

        你也可以这样写:

        SqlCommand cmd=new SqlCommand ("insert into time(project,iteration) values (@project, @iteration)", conn);
        cmd.Parameters.AddWithValue("@project",name1.SelectedValue);
        cmd.Parameters.AddWithValue("@iteration",iteration.SelectedValue);
        

        【讨论】:

          【解决方案6】:

          如前所述,您应该分配连接,并且最好还使用 sql 参数,因此您的命令分配将显示为:

              // 3. Pass the connection to a command object
              SqlCommand cmd=new SqlCommand ("insert into time(project,iteration) values (@project, @iteration)", conn); // ", conn)" added
              cmd.Parameters.Add("project", System.Data.SqlDbType.NVarChar).Value = this.name1.SelectedValue;
              cmd.Parameters.Add("iteration", System.Data.SqlDbType.NVarChar).Value = this.name1.SelectedValue;
          
              //
              // 4. Use the connection
              //
          

          通过使用参数,您可以避免 SQL 注入和其他有问题的拼写错误(例如“myproject's”等项目名称)。

          【讨论】:

            【解决方案7】:

            我喜欢将我所有的 sql 连接放在 using 语句中。我认为它们看起来更干净,当你完成它们时,它们会自行清理。我还建议对每个查询进行参数化,这不仅更安全,而且在您需要返回并进行更改时更易于维护。

            // create/open connection
            using (SqlConnection conn = new SqlConnection("Data Source=Si-6\\SQLSERVER2005;Initial Catalog=rags;Integrated Security=SSPI")
            {
                try
                {
                    conn.Open();
            
                    // initialize command
                    using (SqlCommand cmd = conn.CreateCommand())
                    {
            
                        // generate query with parameters
                        with cmd
                        {
                            .CommandType = CommandType.Text;
                            .CommandText = "insert into time(project,iteration) values(@name, @iteration)";
                            .Parameters.Add(new SqlParameter("@name", this.name1.SelectedValue));
                            .Parameters.Add(new SqlParameter("@iteration", this.iteration.SelectedValue));
                            .ExecuteNonQuery();
                        }
                    }
                }
                catch (Exception)
                {
                    //throw;
                }
                finally
                {
                    if (conn != null && conn.State == ConnectionState.Open)
                    {
                        conn.Close;
                    }
                }
            }
            

            【讨论】:

            • 什么是“oCn”,为什么会有“with”或者它在做什么?
            • “with”是我很懒,没有输入 cmd。对于接下来的五个命令,oCn 是一个错字……这是我工作中连接对象的命名约定。谢谢。
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-05-03
            • 2011-07-22
            相关资源
            最近更新 更多