我使用c#connect到xampp数据库。它可以正常插入但是。 当我尝试在listview中显示数据库中的数据时。所以它会显示"索引超出了数组的范围"。 https://www.dropbox.com/s/whl9jduwwed2swz/8.PNG?dl=1
private void show()
{
string connectionString =
"datasource=127.0.0.1;port=3306;username=root;password=;database=testmg;";
// Select all
string query = "SELECT * FROM car";
MySqlConnection databaseConnection = new MySqlConnection(connectionString);
MySqlCommand commandDatabase = new MySqlCommand(query, databaseConnection);
commandDatabase.CommandTimeout = 60;
MySqlDataReader reader;
try
{
databaseConnection.Open();
reader = commandDatabase.ExecuteReader();
// Success, now list
// If there are available rows
if (reader.HasRows)
{
while (reader.Read())
{
Console.WriteLine(/*reader.GetChar(0) + " - " + reader.GetChar(1) + " - " + */reader.GetChar(2) + " - " + reader.GetChar(3) + " - " + reader.GetChar(4) + " - " + reader.GetChar(5));
//Example to save in the listView1 :
string[] row = {/* reader.GetString(0), reader.GetString(1), */reader.GetString(2), reader.GetString(3), reader.GetString(4), reader.GetString(5) };
var listViewItem = new ListViewItem(row);
listView1.Items.Add(listViewItem);
}
}
else
{
Console.WriteLine("No rows found.");
}
databaseConnection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}