近日,在做一個東東,很多會跟數(shù)據(jù)庫有操作,開始也什么都不懂,遇到很多問題。現(xiàn)在感覺稍有熟悉,把自己的體會和方法拿出來,供大家參考。
另外可以自己定義兩個與數(shù)據(jù)庫取得連接和關(guān)閉連接的函數(shù):
現(xiàn)在我們可以定義具體的數(shù)據(jù)庫操作函數(shù)了。
如果是不用從數(shù)據(jù)庫中取出數(shù)據(jù)的,而只是把數(shù)據(jù)存入數(shù)據(jù)庫中,由于在很大程度上有很大一部分代碼是重復(fù)使用的,所以,我選擇定義另一個數(shù)據(jù)庫操作函數(shù)exeNonQuerySQL(string strCommand):
另外,如果涉及到,從數(shù)據(jù)庫中取出數(shù)據(jù),則可以這樣子:
以上是我個人愚見! 大家多多指正!
首先定義數(shù)據(jù)源等:
private const string strContent = "Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = " +
"../../Food.mdb";
"../../Food.mdb";
另外可以自己定義兩個與數(shù)據(jù)庫取得連接和關(guān)閉連接的函數(shù):
public static OleDbConnection getConnection()
{
OleDbConnection newConnection = new OleDbConnection(strContent);
return newConnection;
}
public static void closeConnection(OleDbConnection conn)
{
if (conn != null)
{
conn.Close();
}
}
這樣就便于操作,而且也比較清晰。(以上的定義都位于DBAccess類){
OleDbConnection newConnection = new OleDbConnection(strContent);
return newConnection;
}
public static void closeConnection(OleDbConnection conn)
{
if (conn != null)
{
conn.Close();
}
}
現(xiàn)在我們可以定義具體的數(shù)據(jù)庫操作函數(shù)了。
如果是不用從數(shù)據(jù)庫中取出數(shù)據(jù)的,而只是把數(shù)據(jù)存入數(shù)據(jù)庫中,由于在很大程度上有很大一部分代碼是重復(fù)使用的,所以,我選擇定義另一個數(shù)據(jù)庫操作函數(shù)exeNonQuerySQL(string strCommand):
private 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;
}
}
這里,我舉一個簡單的例子:delete{
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;
}
}
public Boolean deleteDishByName(string name)
{
string strCommand = "delete from menu where dishName = '" + name + "'";
return exeNonQuerySQL(strCommand);
}
{
string strCommand = "delete from menu where dishName = '" + name + "'";
return exeNonQuerySQL(strCommand);
}
另外,如果涉及到,從數(shù)據(jù)庫中取出數(shù)據(jù),則可以這樣子:
public 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);
}
}
{
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);
}
}
以上是我個人愚見! 大家多多指正!