C# + Sql Server2000 开发WebForm使用try …… catch 来捕捉连接数据库时的异常请问catch 那块的代码如何写的啊???小弟很菜,请大侠指点……谢谢!

解决方案 »

  1.   

    try
    {
    }
    catch(Exception err)
    {
    .......
    }
      

  2.   

    catch要做的几件事:1,给出错误信息,
                       2,关闭错误产生后,没有关闭的相关资源
      

  3.   

    try
      {
         .....
      }
    catch(Exception e)
      {
         ......
      }
    finaly
      {  
         关闭相关资源
      }
      

  4.   

    try
    {
         .....
    }
    catch(Exception e)
    {
         label1.Text = e.Message;//假设label1为web上的一个Label组件
    }
    finaly
    {  
         关闭相关资源
    }
      

  5.   

    我各种的Exception。
    SqlException,IOException综合起来就是Exception
    最后用finally释放所有资源
      

  6.   

    try
    {
       ...
    }
    catch(Exception ex)
    {
        throw ex;
    }try
    {
        ...
    }
    catch
    {
        ....//其他处理
    }
      

  7.   

    try
    {
      我的数据库连接.open();
    }
    catch
    {
      可以直接写 MessageBox.Show("数据库连接异常,请检查!");
    }
    finally
    {
      我的数据库连接.close();
    }
      

  8.   

    public void Connection()
    {
     // 连接字符串
     string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["ConnectionSqlServer"];
     string Sql = "SELECT * FROM Employees";
    // 创建 SqlConnection 和 SqlCommand 对象
     SqlConnection thisConnection = new SqlConnection(ConnectionString);
     SqlCommand thisCommand = new SqlCommand(Sql, thisConnection);
    try
    {
      thisConnection.Open();
      thisCommand.ExecuteNonQuery();
      myLabel.Text = "未出现异常!";
    }
    catch(SqlException ex)
    {
     // 异常信息显示
     myLabel.Text = "<b>数据库执行错误</b><br>";
     myLabel.Text += "错误信息:" + ex.Source + "<br>";
     myLabel.Text += "错误行号:" + ex.LineNumber +"行<br>";
     myLabel.Text += "详细信息:" + ex.Message + "<br>";
    }
    finally
    {
     // 关闭数据库连接
     thisConnection.Close();
    }
    }
      

  9.   

    [WebMethod (EnableSession=true)]
    public DataSet GetDetailByPKID(int svcFrmID,out string msg)
    {
    msg = "";
    if (!SecurityHelper.VerifyCredentials(AppGlobal.UPDSFORM,out msg))
    {
    return null;
    }
    //string sqlRead = "SELECT * FROM ServiceForm WHERE ID=" + svcFrmID;
    string sqlRead = "SELECT A.CustomerName AS CstmName,A.*,B.CustomerName AS EvtCustmName,B.* FROM ServiceForm as A";
    sqlRead += " INNER JOIN CustomerEvent as B";
    sqlRead += " ON A.CustomerEventID = B.ID";
    sqlRead += " WHERE A.ID =" + svcFrmID;
    try
    {
    DataSet ds = new DataSet();
    string[] svcFrmDetail = new string[] {"ServiceFormDetail"};
    SqlHelper.FillDataset(SqlHelper.CONN_STRING,CommandType.Text,sqlRead,ds,svcFrmDetail);
    return ds;
    }
    catch(Exception ex)
    {
    msg = ex.Message.Trim();
    return null;
    }
    }