To close or not to close connection in database

后端 未结 7 1007
名媛妹妹
名媛妹妹 2021-01-17 21:15

I work with Windows-Mobile and Windows-CE using SqlCE and I dont know what better to do.

To open connection when the program open, run any

7条回答
  •  一整个雨季
    2021-01-17 21:33

    If worry about data lost because you are not calling Close() frequently, you can execute your code within a transaction that commits changes to disk immediately:

    using (SqlCeTransaction transaction = this.connection.BeginTransaction())
    {
        using (SqlCeCommand command = new SqlCeCommand(query, connection))
        {
            command.Transaction = transaction;
            command.ExecuteNonQuery();
        }
        transaction.Commit(CommitMode.Immediate);
    }
    

    Of course, there is still some performance lost when using CommitMode.Immediate too frequently.

提交回复
热议问题