//查询排量方法
private void Selpinpai(string pai)
{
string sql =string.Format("select * from carsinfo where discharge='%{0}%'",pai);
try
{
DataSet ds = new DataSet();
SqlCommand command = new SqlCommand(sql, DBHelper.connection);
DBHelper.connection.Open();
SqlDataReader reader = command.ExecuteReader();
ds.Tables["CarsInfo"].Rows.Clear();
while (reader.Read())
{
datagridview1.DataSource = ds.Tables["CarsInfo"];
}
reader.Close(); }
catch (Exception ex)
{
Console.Write(ex.Message);
}
finally
{
DBHelper.connection.Close();
} }我是从数据库查出数据,先清空DataGridView然后将查出的内容填充到控件中,目前控件没有反应,sql没问题,传参也没问题。
private void Selpinpai(string pai)
{
string sql =string.Format("select * from carsinfo where discharge='%{0}%'",pai);
try
{
DataSet ds = new DataSet();
SqlCommand command = new SqlCommand(sql, DBHelper.connection);
DBHelper.connection.Open();
SqlDataReader reader = command.ExecuteReader();
ds.Tables["CarsInfo"].Rows.Clear();
while (reader.Read())
{
datagridview1.DataSource = ds.Tables["CarsInfo"];
}
reader.Close(); }
catch (Exception ex)
{
Console.Write(ex.Message);
}
finally
{
DBHelper.connection.Close();
} }我是从数据库查出数据,先清空DataGridView然后将查出的内容填充到控件中,目前控件没有反应,sql没问题,传参也没问题。
奥,使用SqlDataAdapter的作用和SqlDataReader一样吗?也可以根据查询语句进行查询吗?
{
string sql =string.Format("select * from carsinfo where discharge='%{0}%'",pai);
try
{
DataSet ds = new DataSet();
SqlCommand command = new SqlCommand(sql, DBHelper.connection);
DBHelper.connection.Open();
SqlDataReader reader = command.ExecuteReader();
datagridview1.DataSource =reader;
reader.Close();
DBHelper.connection.close(); }
catch (Exception ex)
{
Console.Write(ex.Message);
}
finally
{
DBHelper.connection.Close();
} }
//查询排量方法
private void Selpinpai(string pai)
{
string sql =string.Format("select * from carsinfo where discharge='%{0}%'",pai);
try
{
DataSet ds = new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter(sql,DBHelper.connection);
DBHelper.connection.Open();
adapter.Fill(ds,"CarsInfo");
datagridview1.DataSource =ds.Tables["CarsInfo"];
}
catch (Exception ex)
{
Console.Write(ex.Message);
}
finally
{
DBHelper.connection.Close();
} }为什么我写成这样,但是查询的时候DataGridView中还是没有数据显示