近日,在做一個(gè)東東,很多會(huì)跟數(shù)據(jù)庫(kù)有操作,開(kāi)始也什么都不懂,遇到很多問(wèn)題。現(xiàn)在感覺(jué)稍有熟悉,把自己的體會(huì)和方法拿出來(lái),供大家參考。

首先定義數(shù)據(jù)源等:
undefinedprivate const string strContent = "Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = " +
            
"../../Food.mdb";

另外可以自己定義兩個(gè)與數(shù)據(jù)庫(kù)取得連接和關(guān)閉連接的函數(shù):
undefinedpublic static OleDbConnection getConnection()
        {
            OleDbConnection newConnection 
= new OleDbConnection(strContent);
            
return newConnection;
        }

        
public static void closeConnection(OleDbConnection conn)
        {
            
if (conn != null)
            {
                conn.Close();
            }
        }
這樣就便于操作,而且也比較清晰。(以上的定義都位于DBAccess類(lèi))

現(xiàn)在我們可以定義具體的數(shù)據(jù)庫(kù)操作函數(shù)了。
如果是不用從數(shù)據(jù)庫(kù)中取出數(shù)據(jù)的,而只是把數(shù)據(jù)存入數(shù)據(jù)庫(kù)中,由于在很大程度上有很大一部分代碼是重復(fù)使用的,所以,我選擇定義另一個(gè)數(shù)據(jù)庫(kù)操作函數(shù)exeNonQuerySQL(string strCommand):
undefinedprivate Boolean exeNonQuerySQL(string strCommand)
        {
            conn 
= DBAccess.getConnection();
            
if (conn != null)
            {
                
try
                {
                    conn.Open();
                    OleDbCommand cmd 
= new OleDbCommand(strCommand, conn);

                    cmd.ExecuteNonQuery();

                    
return true;
                }
                
catch (Exception e)
                {
                    
return false;
                }
                
finally
                {
                    conn.Close();
                }
            }
            
else
            {
                
return false;
            }
        }
這里,我舉一個(gè)簡(jiǎn)單的例子:delete
undefinedpublic Boolean deleteDishByName(string name)
        {
            
string strCommand = "delete from menu where dishName = '" + name + "'";

            
return exeNonQuerySQL(strCommand);
        }

另外,如果涉及到,從數(shù)據(jù)庫(kù)中取出數(shù)據(jù),則可以這樣子:
undefinedpublic Dish getDishbyName(string str)
        {
            Dish dish 
= null;
            
string strCommand = "select comboID, dishName, price from menu where menu.dishName = '"+str+"'";

            conn 
= DBAccess.getConnection();
            
if (conn != null)
            { 
                
try
                {
                    conn.Open();
                    OleDbCommand cmd 
= new OleDbCommand(strCommand, conn);
                    OleDbDataReader reader;
                    reader 
= cmd.ExecuteReader();

                    
if (reader.HasRows)
                    {
                        dish 
= new Dish();
                        reader.Read();
                        dish.Id 
= Int32.Parse(reader["comboID"].ToString());
                        dish.Name 
= reader["dishName"].ToString();
                        dish.Price 
= Decimal.Parse(reader["price"].ToString());
                    }
                }
catch(Exception e)
                {
                    MessageBox.Show(e.Message);
                }
                
finally
                {
                    DBAccess.closeConnection(conn);
                }
            }

以上是我個(gè)人愚見(jiàn)! 大家多多指正!