应用程序——一个文本块,一个按钮。+ SQL 数据库——一张表。当我单击按钮时,我正在尝试从 sql 读取到文本块,但它不起作用。
private void nextButton_Click(object sender, RoutedEventArgs e)
{
GetSqlData();
}
private void GetSqlData()
{
string connectionString = @"Data Source=Jama-Dharma\sqlexpress;Initial Catalog=Cars;Integrated Security=True";
SqlConnection sqlConnection = new SqlConnection(connectionString);
using (sqlConnection)
{
string sqlQuery = @"SELECT c.Name FROM CarsCatalog c";
SqlCommand sqlCommand = new SqlCommand(sqlQuery, sqlConnection);
sqlConnection.Open();
SqlDataReader sqlReader = sqlCommand.ExecuteReader();
while (sqlReader.Read())
{
nameTextBlock.Text = sqlReader.GetString(0);
}
sqlConnection.Close();
}
}
如何在单击按钮时,从 SQL 中获取下一个 ID 值。