在一个按钮事件中调用一个向数据库中插入记录的方法。在方法中try打开数据库并插入记录,遇到异常直接抛出,最后关闭数据库。
在按钮事件函数中显示所遇到的异常的信息,并直接返回(这样不会因为这个异常使得程序死掉),各位帮我看看这样是否合理,具体程序如下
         protected void InsertStudentInfo()
{
//
SqlConnection conn = new SqlConnection(GlobalVar.connString);
//
SqlCommand cmd = new SqlCommand("spStudentInfoInsert", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@studentNO", SqlDbType.Char, 12).Value = studentNO.Text.ToString().Trim();
// 其他存储过程参数……
// 执行操作
try
{
conn.Open();
cmd.ExecuteNonQuery();
}
catch(SqlException ex)
{
throw(ex);
}
finally
{
conn.Close();
}

} private void button1_Click(object sender, System.EventArgs e)
{
// 如果验证输入格式有错误直接返回。
if(ValidateInput() == 0)
return; // 向数据库中插入记录
try
{
InsertStudentInfo();
}
catch(SqlException ex)
{
MessageBox.Show(ex.Message.ToString());
return;
}
finally
{
}
}

解决方案 »

  1.   

    按你的意思, 你把这句:
    catch(SqlException ex)
    {
        throw(ex);
    }去掉试一下.
      

  2.   

    个人认为这样似乎更合理一点
             protected void InsertStudentInfo()
    {
    //
    SqlConnection conn = new SqlConnection(GlobalVar.connString);
    //
    SqlCommand cmd = new SqlCommand("spStudentInfoInsert", conn);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.Add("@studentNO", SqlDbType.Char, 12).Value = studentNO.Text.ToString().Trim();
    // 其他存储过程参数……
    // 执行操作
    try
    {
    conn.Open();
    cmd.ExecuteNonQuery();
    }
    catch(SqlException ex)
    {
                                         MessageBox.Show(ex.Message.ToString());
    throw(ex);
    }
    finally
    {
    conn.Close();
    }

    } private void button1_Click(object sender, System.EventArgs e)
    {
    // 如果验证输入格式有错误直接返回。
    if(ValidateInput() == 0)
    return; // 向数据库中插入记录
    InsertStudentInfo();
    }
      

  3.   

    catch(SqlException ex)
    {
        throw(ex);
    }
    放了这句的执行效果和没放一样,但是比没放效率低很多。
      

  4.   

    看你的代码设计上就有些问题,把数据层代码和UI层代码放在一起了。
    catch(SqlException ex)
    {
    MessageBox.Show(ex.Message.ToString());//这一行不应该写在这里,
    throw(ex);//这样写还不如把整个catch块去掉,只留下try 和finally.不过你可以修改成throw new Exception("InsertStudentInfo()方法出错",ex);
    }还有就是你的代码写的不够健壮,如果conn对象没有打开呢?
    参考例子,
    public void DeleteCategory(int categoryID) 
            {
                SqlConnection conn = new SqlConnection( sqlDataConnString );
                SqlCommand comm = new SqlCommand("DeleteCategory", conn);
                comm.CommandType = CommandType.StoredProcedure;            SqlParameter param = comm.Parameters.Add(new SqlParameter("@CategoryID", SqlDbType.Int));
                comm.Parameters[0].Value = categoryID;            try
                {
                    conn.Open();
                    comm.ExecuteNonQuery();
                }
                finally
                {
                    if (conn.State != ConnectionState.Closed)
                        conn.Close();
                }
            }
      

  5.   

    多谢各位兄弟的回答。
    zhzuo(秋枫)兄弟所指的把数据层代码和UI层代码分离,意思是不是要专门建一个类,用于处理这个数据库的表?然后再在这个ui中用这个类的对象来处理数据库信息?其实这个项目很小,暂时只是涉及到4个表的操作,10几天就可以完成了,所以偷了一点懒