//***Access(ODBC Version)
//[OdbcConnection->OdbcDataAdapter->DataSet->dataGridView1]
string strSQL =“Select * from table”;
try
{
IDbConnection dbConn = new System.Data.Odbc.OdbcConnection(@”Driver={Microsoft Access Driver (*.mdb)};DBQ=|DataDirectory|\database.mdb”);
dbConn.Open();
IDbDataAdapter dbAdapter = new System.Data.Odbc.OdbcDataAdapter(strSQL, (System.Data.Odbc.OdbcConnection)dbConn);
System.Data.DataSet dbSet = new System.Data.DataSet();
dbAdapter.Fill(dbSet);
dataGridView1.DataSource = dbSet.Tables[0];
}
catch (System.Data.Odbc.OdbcException ex)
{
MessageBox.Show(ex.ToString());
}
//***Access(ODBC Version)
//[OdbcConnection->OdbcCommand->DataTable->dataGridView1]
int i;
System.Data.DataTable dt = new DataTable();
System.Data.DataRow dr;
string strSQL =“Select * from table”;
try
{
IDbConnection dbConn = new System.Data.Odbc.OdbcConnection(@”Driver={Microsoft Access Driver (*.mdb)};DBQ=|DataDirectory|\database.mdb”);
dbConn.Open();
IDbCommand dbCommand = new System.Data.Odbc.OdbcCommand(strSQL, (System.Data.Odbc.OdbcConnection)dbConn);
IDataReader dbReader = dbCommand.ExecuteReader();
//
for (i = 0; i < dbReader.FieldCount; i++)
dt.Columns.Add(newDataColumn(dbReader.GetName(i)));
//
while (dbReader.Read())
{
dr = dt.NewRow();
for (i = 0; i < dbReader.FieldCount; i++)
dr[i] = dbReader[i];
dt.Rows.Add(dr);
}
}
catch (System.Data.Odbc.OdbcException ex)
{
MessageBox.Show(ex.ToString());
}
dataGridView1.DataSource = dt;
//***Access(OleDB Version)
//[OleDbConnection->OleDbDataAdapter->DataSet->dataGridView1]
string strSQL=“Select * from table”;
try
{
IDbConnection dbConn = new System.Data.OleDb.OleDbConnection(@”Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\database.mdb;Persist Security Info=True”);
IDbDataAdapter dbAdapter = new System.Data.OleDb.OleDbDataAdapter(strSQL, (System.Data.OleDb.OleDbConnection)dbConn);
System.Data.DataSet dbSet = new System.Data.DataSet();
dbAdapter.Fill(dbSet);
dataGridView1.AutoGenerateColumns = true;
dataGridView1.DataSource = dbSet.Tables[0];
}
catch (System.Data.OleDb.OleDbException ex)
{
MessageBox.Show(ex.ToString());
}
//***Access(OleDB Version)
//[OleDbConnection->OleDbCommand->DataTable->dataGridView1]
int i;
System.Data.DataTable dt = new DataTable();
System.Data.DataRow dr;
string strSQL =“Select * from table”;
try
{
IDbConnection dbConn = new System.Data.OleDb.OleDbConnection(@”Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\database.mdb;Persist Security Info=True”);
dbConn.Open();
IDbCommand dbCommand = new System.Data.OleDb.OleDbCommand(strSQL, (System.Data.OleDb.OleDbConnection)dbConn);
IDataReader dbReader = dbCommand.ExecuteReader();
//
for (i = 0; i < dbReader.FieldCount; i++)
dt.Columns.Add(newDataColumn(dbReader.GetName(i)));
//
while (dbReader.Read())
{
dr = dt.NewRow();
for (i = 0; i < dbReader.FieldCount; i++)
dr[i] = dbReader[i];
dt.Rows.Add(dr);
}
}
catch (System.Data.Odbc.OdbcException ex)
{
MessageBox.Show(ex.ToString());
}
dataGridView1.DataSource = dt;
[教學]ASP.net精靈式的增修刪
[教學]ASPX複製、更名會出現的錯誤和問題除錯
當你在寫ASP.net (ASPX)的網頁程式的時候,如果你的Visual Studio 2008 有出現以下的訊息:
錯誤訊息1:BC30451名稱<xxx控制項>未宣告
WebApplication1WebForm1.aspx.vb(7) :error BC30506: Handles clause requires a WithEvents variable defined in the containing type or one of its base types.
WebApplication1WebForm1.aspx.vb(9) :error error BC30451: Name ‘ TextBox1’ is not declared.
WebApplication1WebForm1.aspx.vb(9) :error BC30451: 名稱 ‘ TextBox1’ 未宣告。(中文版會出線的訊息) 閱讀全文〈[教學]ASPX複製、更名會出現的錯誤和問題除錯〉
PowerPoint 的多重螢幕分割功能
在使用 PowerPoint 做簡報的時候,有個很方便的小功能,可以讓自己的螢幕顯示備忘錄的內容,而另外輸出的大螢幕則顯示原本的簡報內容。
這是個滿實用的功能,不過好像不多人知道該怎麼用,所以在此介紹一下。
雖然 PowerPoint 2000 似乎也有類似的設定,不過在我的電腦上沒辦法正確使用這個功能,故以下是以我實驗成功的 Microsoft PowerPoint 2003 來示範 (Microsoft PowerPoint 2007也是有保留這個功能,只是要到適當的地方做調整)。
[心得]ASP.net 的For迴圈Bug
使用環境:Microsoft Visual Web Developer 2008 Express Edition
使用語言:ASP.net (Visual Basic)
我們好常用的For迴圈,一般來說是可以會更新終止值的(我暫時取叫 n )
但在這裡出現了個很奇特的狀況,ASP.net 的For迴圈,終止值不會被更新
——————————————————————————————————————
在C語言裡,我們的For迴圈是由while所改良而成,寫法如下
int i, n; n=10; for(i=0;i<=n;i++) { printf(“%d\n”, i); } |
以上大略的意思是,初始化 i 為0,若 i 還是小於或等於10的話,就繼續迴圈
執行完裡面的敘述後(就是印出 i 值),i 自己加1
如果用while寫,應該是長這樣子的
int i, n; i=0; n=10; while(i<=n) { printf(“%d\n”, i); i++; } |
——————————————————————————————————————
回到ASP.net (Visual Basic)裡,我寫了幾個測試程式,用好幾個按鈕來執行
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click Response.Write(“測試 n 值變小<br>”) Dim i, n As Integer n = 10 For i = 0 To n If i = 5 Then n = 8 End If Response.Write(“i=” & CStr(i) & ” “ & “n=” & CStr(n) & “<br>” & vbCrLf) Next End Sub
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click Response.Write(“測試 n 值變大<br>”) Dim i, n As Integer n = 10 For i = 0 To n If i = 5 Then n = 20 End If Response.Write(“i=” & CStr(i) & ” “ & “n=” & CStr(n) & “<br>” & vbCrLf) Next End Sub
Protected Sub Button3_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button3.Click Response.Write(“測試 i 值變小<br>”) Dim i, n As Integer Dim flag As Boolean n = 10 For i = 0 To n If i = 7 And flag <> True Then i = 0 flag = True End If Response.Write(“i=” & CStr(i) & ” “ & “n=” & CStr(n) & “flag=” & CStr(flag) & “<br>” & vbCrLf) Next End Sub
Protected Sub Button4_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button4.Click Response.Write(“測試 i 值變大<br>”) Dim i, n As Integer n = 13 For i = 0 To n If i = 2 Then i = 7 End If Response.Write(“i=” & CStr(i) & ” “ & “n=” & CStr(n) & “<br>” & vbCrLf) Next End Sub
Protected Sub Button5_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button5.Click Response.Write(“測試 n 值變小(while版)<br>”) Dim i, n As Integer n = 10 i = 0 While i <= n If i = 5 Then n = 8 End If Response.Write(“i=” & CStr(i) & ” “ & “n=” & CStr(n) & “<br>” & vbCrLf) i += 1 End While End Sub
Protected Sub Button6_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button6.Click Response.Write(“測試 n 值變大(while版)<br>”) Dim i, n As Integer n = 10 i = 0 While i <= n If i = 5 Then n = 20 End If Response.Write(“i=” & CStr(i) & ” “ & “n=” & CStr(n) & “<br>” & vbCrLf) i += 1 End While End Sub |
執行結果:
測試 n 值變小 |
測試 n 值變大 |
測試 i 值變小 |
測試 i 值變大 |
測試 n 值變小(while版) |
測試 n 值變大(while版) |
從上面的程式執行結果看到,測試 n 值變小的時候,本來已經更改成8次就結束了
但程式還是執行10次。在while中同個寫法,就不會有這個問題。
測試n 值變大的時候也是一樣,應該程式應該迴圈執行滿20次,但只有執行10次
所以要注意For迴圈這個特殊的BUG,以避免資料有缺失或是索引超出範圍,怎麼抓都抓不到的BUG。
(註記1:在Visual Studio 2008 裡,在IDE環境裡複製程式碼,到Word 2007貼上,雖然有漂亮的程式碼顏色,但要注意中文字跟中文字之間夾雜的英文或數字會消失不見,關於這個BUG,改天再截圖給大家)
(註記2:同樣的方式貼上到FrontPage 2003,中文字會全部消失不見,這個嚴重的BUG也是其中之一,有空再實驗貼到哪裡才會正常顯示)
[微軟大臭蟲] Visual C++ 2008 的複製掉字問題
這真的要好好寫一下了,有程式搞得這樣的嗎?
這問題出在,當我把東西複製到VC++ 2008,顯示一切正常
當要複製貼上到word時,雖然有很漂亮的原始碼上色
但所有接在中文字附近的數字,都會消失不見(掉字)
這還真的很苦惱耶!
本想說有美麗的程式碼顏色的說