The C# using statement, SQL, and SqlConnection

前端 未结 5 1060
刺人心
刺人心 2020-12-01 18:53

Is this possible using a using statement C# SQL?

private static void CreateCommand(string queryString,
    string connectionString)
{
    using (SqlConnectio         


        
相关标签:
5条回答
  • 2020-12-01 19:30

    Yes, you can put the using block in a try block, and the following catch will catch any errors related to the try block.

    0 讨论(0)
  • 2020-12-01 19:37

    It's possible to do so in C# (I also see that code is exactly shown in MSDN http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executenonquery.aspx). However, if you need to be defensive and for example log potential exceptions which would help troubleshooting in a production environment, you can take this approach:

    private static void CreateCommand(string queryString,
    string connectionString)
    {
        using (SqlConnection connection = new SqlConnection(
               connectionString))
        {
            try
            {
                SqlCommand command = new SqlCommand(queryString, connection);
                command.Connection.Open();
                command.ExecuteNonQuery();
            }
            catch (InvalidOperationException)
            {
                //log and/or rethrow or ignore
            }
            catch (SqlException)
            {
                //log and/or rethrow or ignore
            }
            catch (ArgumentException)
            {
                //log and/or rethrow or ignore
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-01 19:42

    Add a unique index to the database for the fields and catch the error.

    Don't reinstantiate the SQL Connection for each row. Opening and closing a connection is resource intensive. Try something like this:

    protected void btn_insert_Click(object sender, EventArgs e)
    {
        string connStr = "your connection string";
        SqlCommand cmd;
    
        using (SqlConnection con = new SqlConnection(connStr))
        {
            con.Open();
            foreach (GridViewRow g1 in GridView1.Rows)
            {
                try
                {
                    cmd = new SqlCommand("command text", con);
                    cmd.ExecuteNonQuery();
                }
                catch (SqlException sqlEx)
                {
                    //Ignore the relevant Sql exception for violating a sql unique index
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-01 19:43

    If you want to catch any error then you'll need to wrap everything in try - catch block. using blocks simply ensure that non-managed resources are disposed, they cannot handle exceptions.

    Also,SqlCommand implements IDisposable, so I'd suggest putting that in a using block as well.

    0 讨论(0)
  • 2020-12-01 19:51

    Just write it out explicitely:

    SqlConnection connection = new SqlConnection(connectionString);
    try
    {
        using (SqlCommand command = new SqlCommand(queryString, connection))
        {
            command.Connection.Open();
            command.ExecuteNonQuery();
        }
    }
    catch (Exception e)
    {
        // ...handle, rethrow. Also, you might want to catch
        // more specific exceptions...
    }
    finally
    {
        connection.Close();
    }
    
    0 讨论(0)
提交回复
热议问题