2012-07-18 2 views
1

원격 mysql 데이터베이스에 레코드를 추가하고 그 후에 추가되었는지 확인하고 싶습니다. 그래서이 특정 레코드를 선택하려고합니다.C# .net winforms로 mysql 데이터베이스에서 선택 - 응답을 확인하는 방법?

내 코드 :

public void Select(string filename) 
    { 
     string query = "SELECT * FROM banners WHERE file = '"+filename+"'"; 

     //open connection 
     if (this.OpenConnection() == true) 
     { 
      //create command and assign the query and connection from the constructor 
      MySqlCommand cmd = new MySqlCommand(query, connection); 

      //Execute command 
      cmd.ExecuteNonQuery(); 

      //close connection 
      this.CloseConnection(); 
     } 
    } 

방법 서버의 응답을 확인하는? 또는 선택한 레코드? C# windows forms 앱입니다.

+0

당신은 SqlDataReader 개체 또는 SqlDataAdapter를하여 하나의 데이터로 가득 할 DataTable을 사용할 수 있습니다. 이제 datatable 객체에 대한 rowcount 만 확인하십시오. –

+0

파일 이름이 새로 추가 된 레코드입니까? –

답변

1

이 시도 :

public void Select(string filename) 
{ 
     string query = "SELECT * FROM banners WHERE file = '"+filename+"'"; 

     //open connection 
     if (this.OpenConnection() == true) 
     { 
       //create command and assign the query and connection from the constructor 
       MySqlCommand cmd = new MySqlCommand(query, connection); 

       //Get the DataReader from the comment using ExecuteReader 
          MySqlDataReader reader = cmd.ExecuteReader(); 
       while (myReader.Read()) 
       { 
        //Use GetString etc depending on the column datatypes. 
        Console.WriteLine(myReader.GetInt32(0)); 
       } 

       //close connection 
       this.CloseConnection(); 
     } 
} 

확인 대한 추가 정보를 원하시면,이 URL : http://msdn.microsoft.com/en-us/library/haa3afyz(v=vs.100).aspx

2

을 대신 또 다른 왕복을 만드는 (추가로 선택) 당신이

SELECT LAST_INSERT_ID(); 
에 의해 문을 삽입 확장 할 수 있습니다

삽입 명령을 실행하십시오.

command.ExecureQuery(); 

마지막으로 삽입 한 레코드의 ID를 가져옵니다.

INSERT 문 자체에 대한 command.ExecuteNonQuery()을 실행할 때 당신은 그것을 할 수 MySQL Information Functions

관련 문제