2017-09-08 3 views
0

J'essaie d'afficher les enregistrements de la base de données après la modification de la source de données de chaîne de connexion. Voici mon code:Modifier la connexion à la base de données et mettre à jour datagridview

m_dbConnection = new SQLiteConnection("Data Source=" + connFile + "; Version=3; FailIfMissing=True; Foreign Keys=True;"); 
getTable("SELECT * FROM tbl_programs WHERE int_is" + 
    lastDisplayedBuild.ToString() + 
    "=1 ORDER BY txt_programName;"); 

Et mes variables globales sont:

private SQLiteConnection m_dbConnection; 
private SQLiteDataAdapter sqliteDataAdapter = new SQLiteDataAdapter(); 
private DataTable dataTable = new DataTable(); 
private BindingSource bindingSource = null; 

Comment puis-je afficher de nouveaux records en datagridview?

Répondre

0

utilisé cette fonction pour

public DataTable getData(string sqlCommand) 
{ 
    DataTable dt = new DataTable(); 
    using (var con = new SQLiteConnection { ConnectionString = "your connection string" }) 
    { 
     using (var command = new SQLiteCommand { Connection = con }) 
     { 
      if (con.State == ConnectionState.Open) 
       con.Close(); 

      con.Open(); 
      command.CommandText = sqlCommand; 

      dt.Load(command.ExecuteReader()); 

     } 
     return dt; 
    } 
} 


//Call to load your Data 
public void loadData() 
{ 
    this.DatagridView.DataSource = getData("Command string here"); 
}