2011-05-03 5 views
31

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

    } 
+0

SqlConnection, SqlCommand 및 SqlReader 개체는 관리되지 않는 리소스를 사용하므로 일회용 개체이므로 작업이 완료 될 때 처분하는 것이 좋습니다. 코드를보다 읽기 쉽게하려면 using 지시문을 사용하면됩니다. – Beatles1692

+0

이러한 대답이 맞습니다. 동의해야합니다. 생성 된 연결로 sqlcommand 연결 속성을 초기화해야합니다. – Saleh

답변

54

사용하는 것입니다 이것과 패스 연결 개체 :

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

정보에 대한 안녕하세요 감사합니다 .. 나는 C에서 HTML 컨트롤을 얻는 방법도 또 다른 설명을했다 # – jeni

+0

그것은 뱀이 있다면 .../한숨 – ruffin

6

..

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

SqlCommand cmd=new SqlCommand ("insert into time(project,iteration)values('....이후3210

cmd.Connection = conn; 

희망이 도움이 연결을 할당해야하고, 바람직 대신 SQL 매개 변수를 사용해야합니다 언급 한 바와 같이

2

을 추가, 그래서 당신의 명령 할당을 읽을 것입니다 :

// 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"와 같은 프로젝트 이름이 그 예입니다).

3

을 당신이 쓸 수 있습니다 :

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

나는 using 제표에 내 모든 SQL 연결을 배치 것을 좋아합니다. 나는 그들이 더 청결 해 보이고, 당신이 그들과 함께 할 때 스스로 청소한다고 생각합니다. 모든 쿼리를 매개 변수화하는 것이 더 안전 할뿐만 아니라 돌아와서 변경해야하는 경우 유지 관리가 더 쉽습니다.

// create/open connection 
using (SqlConnection conn = new SqlConnection("Data Source=Si-6\\SQLSERVER2005;Initial Catalog=rags;Integrated Security=SSPI") 
{ 
    try 
    { 
     oCn.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; 
     } 
    } 
} 
관련 문제