从数据库检索返回null而不是添加的值

时间:2017-05-25 00:52:08

标签: c# dataset

SqlConnection cone = new SqlConnection(Properties.Settings.Default.cnx);

void viewdata() {
    SqlDataAdapter adap = new SqlDataAdapter("select movie_name,movie_date,movie_desc from movies", cone);

    DataTable dt = new DataTable();
    adap.Fill(dt);

    dataGridView1.DataSource = dt;
}

我的表有4行,所以我注意到它返回5个空的DataGrid行,即使我确保数据库填充了数据

2 个答案:

答案 0 :(得分:2)

void viewdata() {
    SqlDataAdapter adap = new SqlDataAdapter("select movie_name as movie,movie_date as date,movie_desc as desc from movies", cone);
    DataTable dt = new DataTable();
    adap.Fill(dt);
    dataGridView1.DataSource = dt;
    dataGridView1.DataBind();
}

并将网格的AutoGenerateColumns属性设置为true

答案 1 :(得分:0)

手动填充并获取变量而不是Null

SqlCommand cmd = new SqlCommand("SELECT nom_cinema,addresse,heur_ouverture,heur_fermeture FROM [dbo].[cinema]", con);

                SqlDataReader cv = cmd.ExecuteReader();



                while (cv.Read())
                {

                    dataGridView1.Rows.Add(cv.GetValue(0).ToString(), cv.GetValue(1).ToString(), cv.GetValue(2).ToString(), cv.GetValue(3).ToString());

                }