2016-08-24 44 views
-1

我嘗試使用連接字符串的數據庫之間的連接,該連接字符串位於app.config中。但問題是變量適配器返回null,所以不讀取所需的數據。我發現代碼中沒有錯誤。通過連接字符串進行數據庫連接返回null

DataTable ds; 

string con = System.Configuration.ConfigurationManager.ConnectionStrings["dbConnect"].ConnectionString; 
using (SqlConnection conn = new SqlConnection(con)) 
{ 
    SqlDataAdapter adapter; 
    adapter = new SqlDataAdapter("select a.aaa, m.bbb, m.ccc m.ddd From lst_mdmel_lis_tab m with (nolock) inner join lst_absae_tab a with (nolock) on a.id = m.id where m.bbb >= 30 and m.fff= 35 order by a.aaa", conn); 

    ds = new DataTable(" "); 
    adapter.Fill(ds); 
    dataGridView1.DataSource = ds; 
} 
+2

你檢查,「騙子」的值,當你手動運行SQL多少條記錄,你得到什麼? – BugFinder

+0

是的,我檢查了con的值有正確的。你對另一個問題有什麼意見? – myworld

+0

這個'adapter'變量是'null'的字面上不可能的。類型構造函數會引發異常或創建對象。你需要再看一遍,找出到底發生了什麼。 – jmcilhinney

回答

0
where m.bbb>= '30' and m.fff= '35' 

>=爲數值比較運算符。因此,如果m.bbb是m.fff沿數值,那麼正確的語法是

where m.bbb >= 30 and m.fff= 35 
+0

是的抱歉,這是一個輸入錯誤的代碼看起來像這樣 – myworld

+0

嗯,你應該更新問題,然後顯示你使用的代碼。另外爲什麼你使用'新的DataTable(「」)'爲什麼不使用'新的DataTable()'? – timkly

+0

它只是一個測試新的DataTable()它也不起作用 – myworld