2017-04-13 4 views
0

mysql에서 select에서 save 변수에 큰 문제가있다. 내가가 ExecuteReader, ExecuteNonQuery는, ExecuteScalar는 같은 명령을 많이 시도C#에서 mysql을 변수로 선택 쓰기

string connectionstring = @"****;userid=*****; 
password=***;database=***"; 
cnn = new MySqlConnection(connectionstring); 
cnn.Open(); 
MySqlDataReader reader = null; 
string query_date = "SELECT computer_name from wp_users where user_login = @login"; 
MySqlCommand command2 = new MySqlCommand(query_date, cnn); 
command2.Parameters.AddWithValue("@login", metroTextBox.Text); 
reader = command2.ExecuteReader(); 
while (reader.Read()) 
{ 
    string ColumnName = (string)reader["computer_name"]; 
} 
cnn.Close(); 

:

나는 다음과 같은 코드를 썼습니다. 하지만 그들 중 누구도 근무하고 동일한 오류가 발생하고 있습니다 : enter image description here

실제로 무엇이 잘못되었는지 모릅니다. 나는 많은 것을 검색했고 어떤 형태의 해결책도 찾지 못했습니다. 도와주세요.

당신이 쓴 그리고 난이 어떻게했는지 난 그냥 한 1

편집 : 이전하지만 다른 장소로

 string connectionstring = @"****;userid=*****; 
     password=***;database=***"; 
     cnn = new MySqlConnection(connectionstring); 
     cnn.Open(); 
     MySqlDataReader reader = null; 
     string query_date = "SELECT computer_name from wp_users where user_login = @login"; 
     MySqlCommand command2 = new MySqlCommand(query_date, cnn); 
     command2.Parameters.AddWithValue("@login", metroTextBox.Text); 
     DataTable table = new DataTable("ResultTable"); 
      MySqlDataAdapter adapter = new MySqlDataAdapter(command2); 
      adapter.Fill(table); 

      // This is the important line 
      string result = table.Rows[0].ToString(); 

      cnn.Close(); 

그 같은 오류가. 여기에 무슨 일이 일어나고 있는지 몰라. 추가 정보는 영어로 의미 : 키가 사전에 존재하지 enter image description here

편집 2

난 그냥 코드를 업데이트하려고하면 재미있는입니다 :

string connectionstring = @"****;userid=*****; 
    password=***;database=***"; 
    cnn = new MySqlConnection(connectionstring); 
    cnn.Open(); 
    MySqlDataReader reader = null; 
    string upd = "UPDATE w_users Set computer_name = CURRENT_DATE where user_login = @login"; 
    MySqlCommand command2 = new MySqlCommand(upd, cnn); 
    command2.Parameters.AddWithValue("@login", metroTextBox.Text); 
    DataTable table = new DataTable("ResultTable"); 
    SqlDataAdapter adapter = new MySqlDataAdapter(command2); 
    adapter.Fill(table); 
    cnn.Close(); 

그리고이 어떤 오류없이 잘 작동 내 테이블을 업데이 트 ... 그것의 요점은 무엇입니까

EDI T 3

내가 ExecuteScalar는 사용하려고 jutr() 여전히 내가 같은 오류가 있습니다 이 enter image description here

+0

Oracles MySQL Connector를 사용하고 있습니까? –

+0

아니요. winforms에서 응용 프로그램을 만들고 mysql 데이터베이스 – Michael

+0

에 연결하려고 시도하면 예외 정보가 도움이 될 수 있습니다. 제발, 여기에 스택 추적 및 내부 예외 세부 사항을 게시하십시오 –

답변

0

이 예외가 결과 집합에 일치하는 키가 없다는 것을, 당신에게하고 싶어. 키는 대소 문자를 구분합니다. 모든 대문자로 열 이름의 철자를 찾으려고 했습니까 : (문자열) 독자 [ "COMPUTER_NAME"]입니까?

데이터베이스는 소문자로 열 이름을 선택 했더라도 열 이름을 대문자로 반환하는 경향이 있습니다. 다음 코드는 당신을 위해 작동하는 경우

+0

리더의 현재 위치를 입력하기 전에 오류가 발생합니다. – Crowcoder

+0

죄송합니다. 스크린 샷에서 예외 위치를 알지 못했습니다. 귀하의 코드를 시도했는데 설명 된 오류를 재현 할 수 없습니다. MySQL 드라이버 설치에 문제가있을 수 있습니다. – ichselber

+0

command2.Parameters.AddWithValue ("@ login", metroTextBox.Text); "@"로 인해이 문제가 발생할 수 있습니다. 시도해 보셨습니까? command2.Parameters.AddWithValue ("login", metroTextBox.Text); – ichselber

0

당신이 MySqlDataAdapter가있는 경우, 다음과 같이합니다

string connectionstring = @"****;userid=*****; 
password=***;database=***"; 
cnn = new MySqlConnection(connectionstring); 
cnn.Open(); 

string query_date = "SELECT computer_name AS `computer_name` FROM wp_users WHERE user_login = @login"; 

MySqlCommand command2 = new MySqlCommand(query_date, cnn); 
command2.Parameters.AddWithValue("@login", metroTextBox.Text); 

DataTable table = new DataTable("ResultTable"); 
MySqlDataAdapter adapter = new MySqlDataAdapter(command2); 
adapter.Fill(table); 

// This is the important line 
string result = table["computer_name"]; 

cnn.Close(); 
+0

메인 주제를보고, 내가 방금 편집, 당신이 문자열 결과 = table [ "computer_name"]; 나는 내부에 오류가 있음을 알 수 없다. – Michael

+0

@Michael 나는 편집을 보았지만 여전히 효과가 없다는 것은 이상하다. 'SHOW CREATE TABLE wp_users;'쿼리를 실행하여 그 결과를 보여줄 수 있습니까? 나는 그 때 당신을 더 도울 수있을 것이다 –

+0

그것의 일 지금 지금보십시오 나는 다만 당신이 나가 당신을 요구할 때 당신이 아니오를 말했더라도 나는 대답을 – Michael