2017-09-08 79 views
0

我試圖在更改連接字符串數據源後顯示數據庫記錄。這裏是我的代碼:更改數據庫連接並更新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;"); 

我的全局變量是:

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

如何顯示在datagridview的新紀錄?

回答

0

使用該功能來

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"); 
}