conn.Open();
try
{
da.SelectCommand.ExecuteNonQuery();
}
catch(Exception ex)
{
Console.Write(ex.Message);
}
finally
{
conn.Dispose();
conn.Close();
} return "1";

解决方案 »

  1.   

    1. make it a method (of course, you probably want to rethink if you need to pass conn too, since you could do da.SelectCommand.Connection.Open()...)string RunCommand(SqlConnection conn, SqlDataAdapter da)
    {conn.Open();
    try
    {
    da.SelectCommand.ExecuteNonQuery();
    }
    catch(Exception ex)
    {
    Console.Write(ex.Message);
    }
    finally
    {
    conn.Dispose();
    conn.Close();
    } return "1";
    }
    2. use a macro, seeVisual Studio .NET Macros for quick insertion of C# Conditional and Iteration Statements
    http://www.codeproject.com/macro/vscsharpmacros.asp