1.连接数据库
2.把数据读到DataTable中
3.遍历DataTable表
public static DataTable ExecuteDataTable(string sql, params SqlParameter[] parameters)
{
using (SqlConnection conn = new SqlConnection(connStr))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
= sql;
cmd.Parameters.AddRange(parameters);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataSet dataset = new DataSet();
adapter.Fill(dataset);
return dataset.Tables[0];
}
}
}
private void button3_Click(object sender, RoutedEventArgs e)
{
DataTable table = SqlHelper.ExecuteDataTable("select * from T_Students where volk=@volk",
new SqlParameter("@volk", "汉"));
foreach (DataRow row in table.Rows)
{
string name = (string)row["Name"];
MessageBox.Show(name);
}
}