2 回答

TA貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超4個贊
您需要執(zhí)行命令。
using (SqlConnection connection = new SqlConnection(connectionstr))
{
connection.Open();
// Delete old entries
SqlTransaction trans = connection.BeginTransaction();
string sql = "Delete from PhilaMethod ";
SqlCommand cmd = new SqlCommand(sql, connection, trans);
cmd.ExecuteNonQuery(); // <--- added this
trans.Commit();
connection.Close();
}

TA貢獻(xiàn)1798條經(jīng)驗(yàn) 獲得超7個贊
您的代碼存在一些問題。您應(yīng)該始終處理一次性物品。通過調(diào)用Dispose()或使用using {}塊,這是首選方式。
1.)您沒有處理您的 SqlCommand 對象您應(yīng)該將它們包裝在 using 語句中以避免此錯誤和可能的內(nèi)存泄漏。
2.) 您沒有處理您的 SqlTransaction 對象您應(yīng)該始終將其包裝在 using 語句中以避免可能的內(nèi)存泄漏。
3.)像其他人已經(jīng)寫過你必須執(zhí)行一個命令。;)
using (SqlConnection connection = new SqlConnection(connectionstr))
{
connection.Open();
// Delete old entries
SqlTransaction trans = connection.BeginTransaction();
string sql = "Delete from PhilaMethod ";
SqlCommand cmd = new SqlCommand(sql, connection, trans);
trans.Commit();
connection.Close();
}
在此代碼中,缺少執(zhí)行。;) 所以你的代碼應(yīng)該如下所示。(未測試)
using (SqlConnection connection = new SqlConnection(connectionstr))
{
connection.Open();
// Delete old entries
using (SqlTransaction trans = connection.BeginTransaction())
{
string sql = "Delete from PhilaMethod ";
using (SqlCommand cmd = new SqlCommand(sql, connection, trans))
{
cmd.ExecuteNonQuery(); // or whatever method you need
}
trans.Commit();
}
connection.Close();
}
- 2 回答
- 0 關(guān)注
- 150 瀏覽
添加回答
舉報(bào)