如果您的按钮在单击时没有执行SQL查询,则可能是因为没有将事件处理程序与按钮关联。 在以下示例中,我们将使用C#代码演示如何将点击事件处理程序与按钮关联,并在单击时执行SQL查询:
private void button1_Click(object sender, EventArgs e) { string connectionString = "Data Source=MyDatabase.db"; string sqlQuery = "SELECT * FROM MyTable";
using (SQLiteConnection connection = new SQLiteConnection(connectionString))
{
using (SQLiteCommand command = new SQLiteCommand(sqlQuery, connection))
{
connection.Open();
using (SQLiteDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
// iterate through the results and do something with them
}
}
}
}
}
在此示例中,我们创建一个名为“button1”的按钮,并在单击时调用名为“button1_Click”的事件处理程序。 在事件处理程序中,我们使用SQLiteConnection和SQLiteCommand对象来执行SQL查询。 在查询中,我们选择名为“MyTable”的表中的所有行并使用SQLiteDataReader读取结果。 最后,我们可以迭代结果并对其执行所需的操作。 请确保将连接字符串更改为适合您的数据库的实际连接字符串。
下一篇:按钮点击问题