为什么在sqlconnection open不抛出异常

luckylf 2003-08-19 10:44:13
我把服务关了了,也不抛出异常,反而在具体操作数据库的时候抛出。
...全文
49 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
雪狼1234567 2003-08-19
  • 打赏
  • 举报
回复
你没有打开它就不抛出,你用conn.open(),如果没异常的话说明连接是好的,
以后的异常不是conn连接的异常
using System; using System.Data; using System.Configuration; using System.Data.OleDb; namespace xxxxx { /// /// Access数据库访问类 /// public class DataAccess { private static OleDbConnection conn = new OleDbConnection(); private static OleDbCommand comm = new OleDbCommand(); private static string asscessPath = @".\PoliScan.mdb"; /// /// 设置Access数据库路径 /// /// 完整的路径字符串 public static void SetAccessPath(string path) { asscessPath = path; } /// /// 打开数据库 /// /// 成功返回 true;失败返回 false;异常时抛出异常 public static bool OpenConnection() { bool retval = false; if (conn.State != ConnectionState.Open) { conn.ConnectionString = @"Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + asscessPath; comm.Connection = conn; try { conn.Open(); if (conn.State == ConnectionState.Open) { retval = true; } } catch (Exception e) { throw new Exception(e.Message); } ............................ ............................ using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.OleDb; using System.Data.SqlClient; /// /// DataBase 的摘要说明 /// public class DataBase : IDisposable { public DataBase() { // // TODO: 在此处添加构造函数逻辑 // } private SqlConnection con; //创建连接对象 #region 打开数据库连接 /// /// 打开数据库连接. /// private void Open() { // 打开数据库连接 if (con == null) { con = new SqlConnection("Data Source = . ;Database = CET ;Integrated Security = SSPI "); } if (con.State == System.Data.ConnectionState.Closed) con.Open(); }
public void AddItem(string cartID, int bookID, int quantity) { // 新建Connection和Command对象 SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]); SqlCommand myCommand = new SqlCommand("ShoppingCartAddItem", myConnection); // 指定命令对象为存储过程 myCommand.CommandType = CommandType.StoredProcedure; // 为存储过程添加参数信息 SqlParameter parameterBookID = new SqlParameter("@BookID", SqlDbType.Int, 4); parameterBookID.Value = bookID; myCommand.Parameters.Add(parameterBookID); SqlParameter parameterCartID = new SqlParameter("@CartID", SqlDbType.NVarChar, 50); parameterCartID.Value = cartID; myCommand.Parameters.Add(parameterCartID); SqlParameter parameterQuantity = new SqlParameter("@Quantity", SqlDbType.Int, 4); parameterQuantity.Value = quantity; myCommand.Parameters.Add(parameterQuantity); // 打开连接并执行操作 myConnection.Open(); myCommand.ExecuteNonQuery(); myConnection.Close(); } /// /// 获取购物车图书总数量 /// /// /// public int GetItemCount(string cartID) { // 新建Connection和Command对象 SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]); SqlCommand myCommand = new SqlCommand("ShoppingCartItemCount", myConnection); // 指定命令类型为存储过程 myCommand.CommandType = CommandType.StoredProcedure; SqlParameter parameterCartID = new SqlParameter("@CartID", SqlDbType.NVarChar, 50); parameterCartID.Value = cartID; myCommand.Parameters.Add(parameterCartID); // 添加输出参数 SqlParameter parameterItemCount = new SqlParameter("@ItemCount", SqlDbType.Int, 4); //指定参数类型为输出参数 parameterItemCount.Direction = ParameterDirection.Output; myCommand.Parameters.Add(parameterItemCount); // 打开连接并执行操作 myConnection.Open(); myCommand.ExecuteNonQuery(); myConnection.Close(); // 通过存储过程的输出参数来返回购物车总图书量 return ((int)parameterItemCount.Value); } /// /// 根据购物车ID获取购物车信息 /// /// 购物车ID /// DataReader:购物车具体信息 public SqlDataReader GetItems(string cartID) { // 新建Connection和Command实例 SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]); SqlCommand myCommand = new SqlCommand("ShoppingCartList", myConnection); // 指定命令为存储过程 myCommand.CommandType = CommandType.StoredProcedure; // 添加参数购物车编号到存储过程 SqlParameter parameterCartID = new SqlParameter("@CartID", SqlDbType.NVarChar, 50); parameterCartID.Value = cartID; myCommand.Parameters.Add(parameterCartID); // 打开连接,执行操作 myConnection.Open(); SqlDataReader result = myCommand.ExecuteReader(CommandBehavior.CloseConnection); // 返回数据读取器 return result; } /// /// 获取购物车中图书的总价 /// /// 购物车编号 /// 总价 public decimal GetTotal(string cartID) { // 新建Connection和Command对象 SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]); SqlCommand myCommand = new SqlCommand("ShoppingCartTotal", myConnection); // 指定命令类型为存储过程 myCommand.CommandType = CommandType.StoredProcedure; // 添加参数到存储过程 SqlParameter parameterCartID = new SqlParameter("@CartID", SqlDbType.NVarChar, 50); parameterCartID.Value = cartID; myCommand.Parameters.Add(parameterCartID); //添加输出总价的参数到存储过程 SqlParameter parameterTotalCost = new SqlParameter("@TotalCost", SqlDbType.Money, 8); parameterTotalCost.Direction = ParameterDirection.Output; myCommand.Parameters.Add(parameterTotalCost); // 打开连接,执行操作 myConnection.Open(); myCommand.ExecuteNonQuery(); myConnection.Close(); // 返回结果 if (parameterTotalCost.Value.ToString() != "") { return (decimal)parameterTotalCost.Value; } else { return 0; } } /// /// 合并购物车:用于合并用户登陆前和登陆后的购物车 /// /// 登陆前的购物车ID /// 登陆后的购物车ID public void MigrateCart(String oldCartId, String newCartId) { // 新建Connection和Command实例 SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]); SqlCommand myCommand = new SqlCommand("ShoppingCartMigrate", myConnection); // 指定操作类型为存储过程 myCommand.CommandType = CommandType.StoredProcedure; // 添加参数到存储过程 SqlParameter cart1 = new SqlParameter("@OriginalCartId ", SqlDbType.NVarChar, 50); cart1.Value = oldCartId; myCommand.Parameters.Add(cart1); SqlParameter cart2 = new SqlParameter("@NewCartId ", SqlDbType.NVarChar, 50); cart2.Value = newCartId; myCommand.Parameters.Add(cart2); // 打开连接,执行操作 myConnection.Open(); myCommand.ExecuteNonQuery(); myConnection.Close(); } /// /// 删除购物车中的图书 /// /// 购物车编号 /// 图书编号 public void RemoveItem(string cartID, int bookID) { // 新建Connection和Command实例 SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]); SqlCommand myCommand = new SqlCommand("ShoppingCartRemoveItem", myConnection); // 指定操作类型为存储过程 myCommand.CommandType = CommandType.StoredProcedure; // 添加参数给存储过程 SqlParameter parameterProductID = new SqlParameter("@ProductID", SqlDbType.Int, 4); parameterProductID.Value = bookID; myCommand.Parameters.Add(parameterProductID); SqlParameter parameterCartID = new SqlParameter("@CartID", SqlDbType.NVarChar, 50); parameterCartID.Value = cartID; myCommand.Parameters.Add(parameterCartID); // 打开连接,执行操作 myConnection.Open(); myCommand.ExecuteNonQuery(); myConnection.Close(); } /// /// 更新购物车中图书信息 /// /// 购物车编号 /// 图书编号 /// 图书数量 public void UpdateItem(string cartID, int productID, int quantity) { // 如果输入数量小于0则抛出异常 if (quantity < 0) { throw new Exception("Quantity cannot be a negative number"); } // 新建Connection和Command实例 SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]); SqlCommand myCommand = new SqlCommand("ShoppingCartUpdate", myConnection); // 指定操作为存储过程 myCommand.CommandType = CommandType.StoredProcedure; // 添加参数给存储过程 SqlParameter parameterProductID = new SqlParameter("@ProductID", SqlDbType.Int, 4); parameterProductID.Value = productID; myCommand.Parameters.Add(parameterProductID); SqlParameter parameterCartID = new SqlParameter("@CartID", SqlDbType.NVarChar, 50); parameterCartID.Value = cartID; myCommand.Parameters.Add(parameterCartID); SqlParameter parameterQuantity = new SqlParameter("@Quantity", SqlDbType.Int, 4); parameterQuantity.Value = quantity; myCommand.Parameters.Add(parameterQuantity); // 打开连接,执行操作 myConnection.Open(); myCommand.ExecuteNonQuery(); myConnection.Close();
private SqlConnection con; //数据库连接、关闭、释放资源 #region 数据库连接、关闭、释放资源 private void Open() { if (con == null) { //根据Web.Config文件中数据库连接串,建立数据连接 con = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ToString ()); } try { //判断连接对象状态,如果是关闭,将其打开 if (con.State == System.Data.ConnectionState.Closed) con.Open(); } catch (System.Data.SqlClient.SqlException E) { //如果出现错误,关闭数据连接,并抛出错误信息 this.Close(); throw new Exception(E.Message); } } public void Close() { if (con != null) { con.Close();//关闭连接 } } public void Dispose() { if (con != null) { con.Dispose();//释放连接资源 con = null; } } #endregion #region 参数处理 public SqlParameter MakeInParam(string ParamName, SqlDbType DbType, int Size, object Value) //参数处理 { SqlParameter param; if (Size > 0) { param = new SqlParameter(ParamName, DbType, Size); } else { param = new SqlParameter(ParamName, DbType); } param.Direction = ParameterDirection.Input; if (Value != null) { param.Value = Value; } return param; } #endregion #region 将参数添加到SqlCommand当中 private SqlCommand CreateCommand(string procName, SqlParameter[] prams) //将参数添加到SqlCommand当中 { this.Open();//确认打开连接 SqlCommand cmd = new SqlCommand(procName, con); cmd.CommandType = CommandType.Text; //执行类型是命令文本。上面的参数procName即为SQL语句 //cmd.CommandType = CommandType.StoredProcedure; //执行类型是存储过程。上面的参数procName即为存储过程名称 //下面依次把参数传入命令文本 if (prams != null) { foreach (SqlParameter parameter in prams) cmd.Parameters.Add(parameter); } return cmd; } #endregion #region 将参数添加到SqlDataAdapter当中 private SqlDataAdapter CreateDataAdapter(string procName, SqlParameter[] prams) //将参数添加到SqlDataAdapter当中 { this.Open(); SqlDataAdapter dap = new SqlDataAdapter(procName, con); dap.SelectCommand.CommandType = CommandType.Text;//执行类型:命令文本 //dap.SelectCommand.CommandType = CommandType.StoredProcedure;//执行类型:存储过程 if (prams != null) { foreach (SqlParameter parameter in prams) { dap.SelectCommand.Parameters.Add(parameter); } } return dap; } #endregion #region 针对查询(带参) public DataSet RunProcReturn(string procName, SqlParameter[] prams, string tbName) //针对查询(带参) { SqlDataAdapter dap = CreateDataAdapter(procName, prams); DataSet ds = new DataSet(); dap.Fill(ds, tbName); this.Close(); //得到执行成功返回值 return ds; } #endregion #region 针对查询(不带参) public DataSet RunProcReturn(string procName, string tbName) //针对查询(不带参) { SqlDataAdapter dap = CreateDataAdapter(procName, null); DataSet ds = new DataSet(); dap.Fill(ds, tbName); this.Close(); //得到执行成功返回值 return ds; } #endregion #region 针对增删改 public int RunProc(string procName, SqlParameter[] prams) //针对增删改 { SqlCommand cmd = CreateCommand(procName, prams); int i = cmd.ExecuteNonQuery(); this.Close(); return i; } #endregion }

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧