programing

ExecuteReader: 연결 속성이 초기화되지 않았습니다.

jooyons 2023. 8. 28. 21:01
반응형

ExecuteReader: 연결 속성이 초기화되지 않았습니다.

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

끝나고SqlCommand cmd=new SqlCommand ("insert into time(project,iteration)values('....더하다

cmd.Connection = conn;

이것이 도움이 되길 바랍니다.

명령 개체에 연결을 할당해야 합니다. 예를 들어...

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

모든 답은 사실입니다.이것은 다른 방법입니다.그리고 나는 이것이 좋습니다.

SqlCommand cmd = conn.CreateCommand()

문자열 concat에 SQL 주입 문제가 있다는 것을 알아야 합니다.매개 변수 사용: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.parameters.aspx

다음과 같이 쓸 수도 있습니다.

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

앞서 언급한 것처럼 연결을 할당해야 하며 명령 할당 내용이 다음과 같이 표시되도록 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"와 같은 프로젝트 이름).

모든 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;
        }
    }
}

언급URL : https://stackoverflow.com/questions/5866046/executereader-connection-property-has-not-been-initialized

반응형