Im new to C# here. 나는 사용자가 월과 날짜를 선택할 수있게 해주는 comboBox 코드를 가지고있다. 사용자가 cmdSend 버튼을 클릭하면 프로그램은 달 & comboBox를 검색하고 dbConnect.Select 클래스 함수를 호출하여 select mysql 문을 수행합니다.콤보 상자에서 선택한 값을 기반으로 데이터베이스에서 데이터를 검색하는 방법은 무엇입니까?
private void cmdSend_Click(object sender, System.EventArgs e)
{
List<string>[] list;
list = dbConnect.Select(month_list.Text, year_list.Text);
printer_info.Rows.Clear();
for (int i = 0; i < list[0].Count; i++)
{
int number = printer_info.Rows.Add();
printer_info.Rows[number].Cells[0].Value = list[0][i];
printer_info.Rows[number].Cells[1].Value = list[1][i];
printer_info.Rows[number].Cells[2].Value = list[2][i];
printer_info.Rows[number].Cells[3].Value = list[3][i];
}
}
검 데이터베이스 클래스 :
public List<string>[] Select(string month,string year)
{
string query = "SELECT * FROM page_counter where month ='" + month + "' AND year ='" + year + "' ;";
//Create a list to store the result
List<string>[] list = new List<string>[4];
list[0] = new List<string>();
list[1] = new List<string>();
list[2] = new List<string>();
list[3] = 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"].ToString() + "");
list[1].Add(dataReader["month"].ToString() + "");
list[2].Add(dataReader["year"].ToString() + "");
list[3].Add(dataReader["page_count"].ToString() + "");
}
//close Data Reader
dataReader.Close();
//close Connection
this.CloseConnection();
//return list to be displayed
return list;
}
else
{
return list;
}
}
그러나이 코드는 누군가가 제발 조언 할 수, 작동하지 않는 이유는 무엇입니까?
편집 :
내가 제안의 코드를 편집 한string query = "SELECT * FROM page_counter where month = @month AND year = @year;";
//Open connection
if (this.OpenConnection() == true)
{
//Create Command
MySqlCommand cmd = new MySqlCommand(query, connection);
cmd.Parameters.AddWithValue("@month",month);
cmd.Parameters.AddWithValue("@year",year);
//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"].ToString() + "");
list[1].Add(dataReader["month"].ToString() + "");
list[2].Add(dataReader["year"].ToString() + "");
list[3].Add(dataReader["page_count"].ToString() + "");
}
//close Data Reader
dataReader.Close();
, 그러나 나는 AddWithValue에 오류가,이 말 : AddWithValue에 대한 정의없이 확장 방법 AddWithValue을 포함하지 않는, 내가 추가 한 Data.MySqlClient 참조는 여전히 동일하게 유지됩니다. 제발 조언.
입니까 ?? –
@ SaghirA.Khatri 그것은 winform C#입니다. – Ren