[問題] C#與資料庫連結!!

看板C_Sharp作者 (弗卡夏)時間13年前 (2010/12/27 14:13), 編輯推噓0(009)
留言9則, 2人參與, 最新討論串1/1
我前陣子問了 宣告數字型態變數於刪除指定的問題!! 後解決的敘述為下: private void button3_Click(object sender, EventArgs e) { string selectCmb = ""; int KeyIN = int.Parse(textBox2.Text); selectCmb = "DELETE FROM data WHERE 學號="+KeyIN; dataGridView1.DataSource = Dummy_DataTable(selectCmb, "data"); dataGridView1.AutoResizeColumns(); } 而後我再練習增新功能: private void button4_Click(object sender, EventArgs e) { string Keyin; string key1 = ""; string key2 = ""; string key3 = ""; key1 = textBox3.Text; key2 = textBox4.Text; key3 = textBox5.Text; Keyin="Insert into data (學號,姓名,居住縣市) values ('"+key1+"' ,'"+key2+"','"+key3+"' )" ; dataGridView1.DataSource = Dummy_DataTable(Keyin, "data"); dataGridView1.AutoResizeColumns(); } 很快的就寫完了!! 但是我這本增新的學號key1 資料屬於字串類型 PS:我將程式改為: private void button4_Click(object sender, EventArgs e) { string Keyin; int key1 = int.Parse(textBox3.Text); string key2 = ""; string key3 = ""; key2 = textBox4.Text; key3 = textBox5.Text; Keyin="Insert into data (學號,姓名,居住縣市) values ("+key1+" ,'"+key2+"','"+key3+"' )" ; dataGridView1.DataSource = Dummy_DataTable(Keyin, "data"); dataGridView1.AutoResizeColumns(); } 結果依然一樣 我想請問一下!! 1.我該怎麼增新的資料中 僅有學號這個資料是數字類型!! 我希望學號是數字類型資料的原因是希望它能夠被按照順序被排序!! 謝謝大家!! -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.127.27.231

12/27 15:06, , 1F
請問你如何判斷新增後的資料不是數字類型?
12/27 15:06, 1F

12/27 15:19, , 2F
資料的排序可以從資料庫中設定、SQL語法(ORDER BY)指定或
12/27 15:19, 2F

12/27 15:22, , 3F
從你的dataGridView1中設定,要是都沒設定是不會自動排序
12/27 15:22, 3F

12/27 15:45, , 4F
老實說我也不清楚是否輸入的資料型態為數字
12/27 15:45, 4F

12/27 15:45, , 5F
正如你所言,我是看我輸入的新資新料是否有按順序排序
12/27 15:45, 5F

12/27 15:49, , 6F
感謝ev大加上order by 後就能依照順序排序了!!
12/27 15:49, 6F

12/27 15:49, , 7F
很抱歉 是我沒注意到!!
12/27 15:49, 7F

12/27 15:55, , 8F
你所改的程式碼是新增數字類型的資料沒錯
12/27 15:55, 8F

12/27 17:06, , 9F
因該沒有錯 因為它會自動排序!!
12/27 17:06, 9F
文章代碼(AID): #1D62w4aK (C_Sharp)