大家安安
小弟在下方的撰寫C#程式碼的時候可以執行
但是開啟其他form時卻出現下方的錯誤警示
程式碼寫在該form的load上
同樣的selectCmd內的程式碼在SSMS上可以執行並且顯示結果
麻煩大家可以告訴我哪裡錯嗎?
程式碼及錯誤圖如下
SqlConnection conn = new SqlConnection("Data Source=DESKTOP-TV1G01E;Initial Catalog=DAFU_DATABASE;Integrated Security=True;Pooling=False");
conn.Open();
string selectCmd = "SELECT VENDOR, MATERAILS, MATERAILS_PROCESSING, WIDTH, COUNT(1) AS QUANTITY, SUM(WEIGHT) AS WEIGHT" +
"FROM 線材明細表" +
"GROUP BY VENDOR, MATERAILS, MATERAILS_PROCESSING, WIDTH;";
SqlDataAdapter da = new SqlDataAdapter(selectCmd, conn);
DataSet ds = new DataSet();
da.Fill(ds);
this.dataGridView1.DataSource = ds.Tables[0];
da.Dispose();
conn.Close();
allenlwh 大大沒意外是正解
另外補充給大大,多利用@
來串接多行字串
就不用字串加來加去
舉例:
string selectCmd = @"
SELECT VENDOR, MATERAILS, MATERAILS_PROCESSING, WIDTH, COUNT(1) AS QUANTITY, SUM(WEIGHT) AS WEIGHT
FROM 線材明細表
GROUP BY VENDOR, MATERAILS, MATERAILS_PROCESSING, WIDTH;
";
而且編譯後系統會自動幫你加上\r\n\t
就不會出現少空白,出現SQL error問題
"FROM 線材明細表" +
"GROUP BY VENDOR, MATERAILS
tablename 和 Group by 中間沒有空白,
變成"FROM 線材明細表GROUP BY VENDOR, MATERAILS",
改成以下這樣應該就可以了
"FROM 線材明細表 " +
"GROUP BY VENDOR, MATERAILS