저는 MySQL 데이터베이스를 처음 사용하기 때문에 Visual Studio C#을 사용하여 데이터베이스에 연결하고 있습니다. 나는 다음과 같은 선택 방법을 가지고있다. 작동하는지 확인하려면 어떻게해야합니까?mySql 데이터베이스가 작동하는지 테스트하는 방법은 무엇입니까?
편집 개폐 연결 방법
//Open connection to database
private bool OpenConnection()
{
try
{
// connection.open();
return true;
}
catch (MySqlException ex)
{
//When handling errors, your application's response based
//on the error number.
//The two most common error numbers when connecting are as follows:
//0: Cannot connect to server.
//1045: Invalid user name and/or password.
switch (ex.Number)
{
case 0:
MessageBox.Show("Cannot connect to server.");
break;
case 1045:
MessageBox.Show("Invalid username/password, please try again");
break;
}
return false;
}
}
//Close connection
private bool CloseConnection()
{
try
{
connection.Close();
return true;
}
catch (MySqlException ex)
{
MessageBox.Show(ex.Message);
return false;
}
}
는
public List<string>[] Select()
{
string query = "SELECT * FROM Questions";
//Create a list to store the result
List<string>[] list = new List<string>[3];
list[0] = new List<string>();
list[1] = new List<string>();
list[2] = new List<string>();
list[3] = new List<string>();
list[4] = new List<string>();
list[5] = new List<string>();
list[6] = new List<string>();
list[7] = new List<string>();
//Open connection
if (this.OpenConnection() == true)
{
//Create Command
MySqlCommand cmd = new MySqlCommand(query, connection);
//Create a data reader and Execute the command
MySqlDataReader dataReader = cmd.ExecuteReader();
//Read the data and store them in the list
while (dataReader.Read())
{
list[0].Add(dataReader["id"] + "");
list[1].Add(dataReader["difficulty"] + "");
list[2].Add(dataReader["qustions"] + "");
list[3].Add(dataReader["c_answer"] + "");
list[4].Add(dataReader["choiceA"] + "");
list[5].Add(dataReader["choiceB"] + "");
list[6].Add(dataReader["choiceC"] + "");
list[7].Add(dataReader["choiceD"] + "");
}
//close Data Reader
dataReader.Close();
//close Connection
this.CloseConnection();
//return list to be displayed
return list;
}
else
{
return list;
}
}
이 방법 위와 같이 확대 개방 접속과 같은 클래스에
선택 방법은 별도 인 모든 데이터베이스 연결 설정을 가지고있는 클래스. 이제는 주 수업에서이 메소드를 호출하여 작동하는지 확인하기 위해 테스트하고 싶습니다. 어떻게해야합니까?
안녕하세요. 도움에 감사드립니다. 이 테스트를 도와 줄 버튼을 만들었습니다. 버튼을 누르면 select 메소드가 실행됩니다. 하지만 MySqlDataReader에서 오류가 발생했습니다. dataReader = cmd.ExecuteReader(); 연결이 유효하고 열려 있어야합니다. – user1781232
그러면 'this.OpenConnection'의 코드가 예상대로 작동하지 않습니다. 아마도 그 방법에 대한 코드를 보여 주어 상황을 이해해야합니다. – Steve
openConnection을 포함하고있는 편집 된 버전의 코드를 살펴보십시오. – user1781232